Ways to use Where-Object in PowerShell

The Where−Object or (alias: Where) in PowerShell is used to filter the data output provided through the pipeline.

To get the Services with the StartType AUTOMATIC and the status STOPPED we can use a script block to filter the output with the Property name, value, and the comparison operator.

Get−Service | Where−Object{($_.StartType −eq 'Automatic') −and ($_.Status −eq 'Stopped')}

You can also use Alias: Where instead of Where−Object.

Get−Service | Where{($_.StartType −eq 'Automatic') −and ($_.Status −eq 'Stopped')}

Other Syntax ‘?’ (Question Mark) can also be used Instead of the Where−Object command.

Get−Service | ?{($_.StartType −eq 'Automatic') −and ($_.Status −eq 'Stopped')}

See also the Where-Object cmdlet in PowerShell

PowerShell
Where-Object

Comments

Leave a Comment

All fields are required. Your email address will not be published.