Powershell Where (Where-Object) Cmdlet: Syntax, Alias, Examples

-

|

Introduction

PowerShell Where (Where-Object) selects objects from a collection based on specified property values. The full name of PowerShell Where is Where-Object. Its alias (short name) is a question mark, ?.

In this guide you will lean the syntax of PowerShell Where, its parameters and some examples.

PowerShell Where-Object Syntax

Starting from PowerShell 3.0, you can construct a PowerShell Where in two ways.

PowerShell Where-Object Syntax (Script block)

You can use a script block to specify the object property and a comparison operator to compare specified values. WhereObject will return values for which the block statement is $TRUE.

| Where-Object  { script block statement }

“|” is PowerShell pipe line operator that sends the collection to PowerShell Where-Objecrt statement.

PowerShell Where-Object Syntax (Comparison statement)

PowerShell Where-Object has a parameter called property that allows you to write a comparison statement.

Here is the syntax

| Where-Object -Property <object-name> -eq <object-value>

You can also run the command without specifying the Property parameter.

| Where-Object <object-name> -eq <object-value>

PowerShell Where-Object Alias

A PowerShell alias is an alternate name or nickname for a cmdlet. The full command of PowerShell Where is Where-Object. Its aliases are where and a question mark, ?.

So, the previous syntax can be written as shown below:

 | Where { script block statement } 

or

 | ? { script block statement } 
| Where -Property <object-name> -eq <object-value>

or

| ? -Property <object-name> -eq <object-value>

PowerShell Where-Object Examples

In this section I will give some examples of how to use the Where-Object statement

Script block Example

In this example, I will use a Where-Object statement to return processes with the name ‘svchost’

Here are the commands:

Get-Process | Where-Object  { $_.ProcessName -eq 'svchost' }

Here is the result in PowerShell:

PowerShell Where-Object Examples script block example

In this example the script block is

{ $_.ProcessName -eq 'svchost' }

$_.ProcessName is the property we want to filter. $_. is an automatic pipeline variable
eq is the comparison operator meaning equal to.
‘svchost’ is the property value

In the example, if ProcessName equals ‘svchost’ it will be returned. Otherwise, it will not be returned.

In the example, Where-Object can be replaced with Where or ?. See the commands below and the results in PowerShell.

Get-Process | Where { $_.ProcessName -eq 'svchost' }
PowerShell Where Examples
Get-Process | ? { $_.ProcessName -eq 'svchost' }
PowerShell Where Examples

Comparison Statement Example

I will repeat the same previous examples. But this time I will use the -property parameter to compare values and return desired results.

You can omit the -property parameter entirely. See the examples below.

Here is the command to return processes with name ‘svchost’

Get-Process | Where-Object ProcessName -eq svchost
Get-Process | Where ProcessName -eq svchost
Get-Process | ? ProcessName -eq svchost
The beauty of using this last method is that you write English statements. It does not look like scripting!

PowerShell Where Examples (Wildcards)

You can use the -Match or -Like parameters of the where-object cndlets to filter objects based on wildcards.

Here are some examples

To find all processes with name starting with po, use the command below:

Get-Process | Where-Object {$_.ProcessName -like "po*"}

The result in PowerShell

Advertisement

Here is the same example using the -Match parameter:

Get-Process | Where-Object {$_.ProcessName -Match "^po.*"}

More examples using multiple conditions:

You can add multiple conditions by combing logical and comparison operators

To return processes with the names wininit OR processes with the name WUDFHost use the command below:

Get-Process | Where-Object { ($_.ProcessName -eq 'wininit') -or ($_.ProcessName -eq
'WUDFHost')}

Here is an example using AND logical operator.

Get-Process | where {($_.ProcessName -notlike "svchost*" -and $_.ProcessName -notlike "Runtime*")}

This will return all processes except any process starting with svchost or Runtime

Conclusion

If you want to filter out some objects from a collection, the Where-Object statement is your friend! I hope this guide simplified this cmdlet for you.

If you have any question or comments use the “Leave a Reply” form at the end of this page. You could also share your experience with the Where-Object statement for other readers to benefit.

Other Helpful Guides

Additional Resources and References

LEAVE A REPLY

Please enter your comment!
Please enter your name here

FEATURED POSTS

How to Share a Folder in Windows 10 (3 Methods)

Introduction This guide demos how to share folder in windows 10. It covers 3 methods. Options...

How to Map Network Drive in Windows 10 (5 Methods)

Introduction This guide demos 5 methods to Map Network Drive in Windows 10. Options to...
How to Install Windows 10 1909 Preview Build

How to Install Windows 10 19H2 Preview Build

Introduction If you are a member of Windows 10 Insider Program you can install Windows 10 19H2 Preview Build....

How to Sign in to Windows 10 with a Microsoft Account

Introduction When you installed Windows 10 you may have created and signed in with a local account. You can...
Disable IPv6 in Windows 10

How to Disable IPv6 in Windows 10 (3 Methods)

Introduction This guide demos 3 methods to disable IPv6 in Windows 10: Disable IPv6 from...

Advertisement

TRENDING POSTS

Remote Desktop Connection

Remote Desktop Connection an Internal Error Has Occurred [Fixed]

Introduction I recently received the error message "Remote Desktop Connection an Internal Error Has Occurred". It was strange because...

Find My Samsung: Register and Use Samsung Find my Mobile

Introduction Ever wondered how you could find your Samsung phone if you lost it? Find my Samsung or Samsung...
What is the Difference Between PowerShell and CMD?

Windows Powershell vs CMD: Differences and Similarities

Introduction This short guide compares Windows PowerShell vs CMD (Windows command prompt). I will cover the history and nature...
Spotify No Longer Supports this Version of Microsoft Edge

Spotify No Longer Supports this Version of Microsoft Edge [Fixed]

Introduction When you open Spotify web player on Microsoft Edge, you may receive the error message "Spotify No Longer...
Windows 10 Won't Boot

Windows 10 Won’t Boot With Black Screen? 3 Ways to Fix It

Why Won't Windows 10 Boot Up? If your Windows 10 stops with a black screen, the first question in...

Advertisement

BEST OF ITECHGUIDES

windows server 2016 router configuration

How to Configure Windows Server 2016 as a Router

Introduction This guide demos Windows Server 2016 router configuration. It walks you though how to configure Windows Server 2016...
Windows defender blocked by Group Policy

Windows Defender Blocked by Group Policy [Fixed]

Introduction If you receive the error message Windows Defender is blocked by group policy, your computer may have been...
How to Calculate Variance in Excel

How to Calculate Variance in Excel

Introduction Variance measures the spread between numbers in a data set. You can calculate variance in Excel using the...
Windows 10 not booting after update

How to Fix Windows 10 Boot Problem After Windows Update

If you experience Windows 10 not booting after an update, it is likely that the update corrupted the boot files.
Excel Count

Excel Count: How to Count in Excel With Examples

Introduction Excel COUNT Function is used for counting items in a worksheet. Excel COUNT also has the conditional function,...

RECENT POSTS

How to Enable Hyper-V in Windows 10 (3 Methods)

How to Enable Hyper-V in Windows 10 (3 Methods)

Introduction This guide demos 3 methods to enable Hyper-V in Windows 10. To install Hyper-V...
RSAT Tools in Windows 10 Explained: Plus How to Install RSAT

RSAT Tools in Windows 10 Explained: Plus How to Install RSAT

Introduction Starting from October 2018 (1809) update, RSAT Tools became part of Windows 10. From this version of Windows...

How to Enable RSAT for Active Directory in Windows 10 (3 Methods)

Introduction This guide demos 3 methods to enable Active Directory in Windows 10. It is not exactly enabling "Active...
How to Install Windows 10 1909 Preview Build

How to Install Windows 10 1909 (19H2) Preview Build

Introduction Windows 10 1909 Preview is available for Windows Insiders. Made available early September, 2019 you have to be...
How to Install RSAT in Windows 10 (3 Methods)

How to Install RSAT in Windows 10 (3 Methods)

Introduction This guide demos how to Install RSAT in Windows 10. Starting with Windows 10...

How to Share a Folder in Windows 10 (3 Methods)

Introduction This guide demos how to share folder in windows 10. It covers 3 methods. Options...
Configure Map Network Drive with Group Policy

Map Network Drive in Windows 10 with Group Policy

Introduction This guide demos how to map network drive with group policy. This guide is...

How to Map Network Drive in Windows 10 (5 Methods)

Introduction This guide demos 5 methods to Map Network Drive in Windows 10. Options to...
How to Download Windows 10 ISO with Media Creation Tool

How to Download Windows 10 ISO with Media Creation Tool

Introduction This guide demos the steps to download Windows 10 ISO. You can download Windows 10 ISO with Media...

How to Install Windows 10 from Network Boot (Via WDS Server)

Introduction This guide demos how to install Windows 10 from network boot. The steps discussed in...

Advertisement

MUST READ

add local user windows 10

How to Add a Local User in Windows 10 (3 Methods)

Introduction This guide demos 3 methods to add a local user in Windows 10. Back...
powershell count

How to Count Objects in PowerShell

Introduction There are so many reasons you may want to count objects in PowerShell. As an example, you may...
bootrec /fixboot access is denied

How to Fix BootRec /FixBoot Access is Denied Error in Windows 10

Introduction BootRec /FixBoot Access is Denied Error in Windows 10? This error is likely caused by corrupt EFI directory.

How to Share a Folder in Windows 10 (3 Methods)

Introduction This guide demos how to share folder in windows 10. It covers 3 methods. Options...
powershell version

4 Ways to Check the Current PowerShell Version on Your PC

Introduction If you are reading this guide because you want to learn how to find the PowerShell version on...

By using this website you agree to accept our Privacy Policy and Terms & Conditions