whereobject powershell commands

Where-Object | Taking on PowerShell one cmdlet at a time | Weekly Blog

Share this post:

This is a part of an on-going blog series written by Adam Gordon. Each week, Adam will walk you through a PowerShell command, showing you when and how to use each one. This week, Adam covers Where-Object. 

When to use Where-Object

The Where-Object cmdlet selects objects that have particular property values from the collection of objects that are passed to it.

Starting in Windows PowerShell 3.0, there are two different ways to construct a Where-Object command.

  • Script block. You can use a script block to specify the property name, a comparison operator, and property value. Where-Object returns all objects for which the script block statement is true.
  • Comparison statement. You can also write a comparison statement, which is much more like natural language.


How to use Where-Object

Get stopped services: 

(1st) Get-Service | Where-Object {$_.Status -eq “Stopped”}

(2nd) Get-Service | where Status -eq “Stopped”

These commands get a list of all services that are currently stopped.

The $_ automatic variable represents each object that is passed to the Where-Object cmdlet.

The (1st) command uses the script block format, the (2nd) command uses the comparison statement format.

Where-Object powershell command


Get processes based on working set:

Get-Process | Where-Object {$_.WorkingSet -GT 25000*1024}
Get-Process | Where-Object WorkingSet -GT (25000*1024)

These commands list processes that have a working set greater than 25,000 kilobytes (KB).

Because the value of the WorkingSet property is stored in bytes, the value of 25,000 is multiplied by 1,024.

The script block and statement syntax are equivalent and can be used interchangeably.

Where-Object powershell command


Use the comparison statement format: 

(1st) Get-Process | Where-Object -Property Handles -GE -Value 1000

(2nd) Get-Process | where Handles -GE 1000

This example shows how to use the new comparison statement format of the Where-Object cmdlet.

The (1st) command uses the comparison statement format. In this command, no aliases are used and all parameters include the parameter name.

The (2nd) command is the more natural use of the comparison command format. The where alias is substituted for the Where-Object cmdlet name and all optional parameter names are omitted.

Where-Object powershell command


Use multiple conditions:

Get-Module -ListAvailable | where {($_.Name -notlike “Microsoft*” -and $_.Name -notlike “PS*”) -and $_.HelpInfoUri}

The command uses the ListAvailable parameter of the Get-Module cmdlet to get all modules on the computer.

A pipeline operator (|) sends the modules to the Where-Object cmdlet, which gets modules whose names do not begin with Microsoft or PS, and have a value for the HelpInfoURI property, which tells PowerShell where to find updated help files for the module.

The comparison statements are connected by the And logical operator.

The example uses the script block command format.

NOTE: Logical operators, such as And and Or, are valid only in script blocks. You cannot use them in the comparison statement format of a Where-Object command.

Where-Object powershell command

Need PowerShell training? Check out ITProTV’s PowerShell online IT training courses.

Leave a Reply

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