in

WindowsWindows Loving ItLoving It

Where PowerShell Windows 10

Where PowerShell Windows 10 is the latest tutorial for PowerShell. It is the tutorial for PowerShell 3.0 the latest tutorial in PowerShell.

PowerShell Where

PowerShell Where – How to use?

The where-object cmdlet is used to select only the particular objects that satisfy a certain condition.  For example, we can use the PowerShell where to filter out certain files that were created after a certain date.

To achieve this, we will make use of a script block.  This script block will contain the property name, a comparison operator, and variable value.

We will get the list of services that have been stopped.  Using the below command.

Get-Service | Where-Object {$_.Status -eq “Stopped”}
PowerShell where
PowerShell where

This produces the output as shown in the image.

where output
where output

We can use the where object and pipe it to another where object also.  We will try to get the Xbox services that are stopped.

We can use the below command to do just that.

Get-Service | Where-Object {$_.Status -eq “Stopped”} | where-object{$_.name -like “xbox*”}
get xbox services
get xbox services

The output for this command is shown below.

xbox services output
xbox services output

There is a quick alias for PowerShell where-object and that is the question mark.  We can use ‘?’ for the purpose of where-object.  We will demonstrate the above two commands.

Get-Service | ? {$_.Status -eq “Stopped”}
using the shorthand
using the shorthand

The above command will produce the same results as the first command.

Now we will change the second command with the help of the shortcut and re-run it.

Get-Service | ? {$_.Status -eq “Stopped”} | ? {$_.name -like “xbox*”}

Get the xbox services using the shorthand
Get the xbox services using the shorthand

This will give the below result.

shorthand command output
shorthand command output

Conclusion:

Today we learned about PowerShell where.  It is used to get the list of objects that satisfies a certain condition.  We can use a script block to specify the condition.  There is a shortcut for using the where-object and that is with the help question mark.  We can specify the question mark where we need the where-object.  We have the examples for that also.

If you need more detailed posts on  PowerShell, please stay tuned to TecKangaroo.

Where PowerShell
  • Author Rating
  • Content Clarity
  • Content Relevance
  • Article Rating
4.6

Leave a Reply

Your email address will not be published. Required fields are marked *

Loading…

0

Comments

0 comments