Advertisements

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.

Advertisements


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


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

  • Was this post Helpful?
  • YesNo

LEAVE A REPLY

Please enter your comment!
Please enter your name here

FEATURED POSTS

How to Print Facebook Messages

How to Print Facebook Messages (Directly or from Microsoft Word)

This Itechguide teaches you how to print Facebook messages. You can print Facebook messages directly from Facebook. Or...
How to Embed YouTube Video on Facebook

How to Embed YouTube Video on Facebook

This Itechguide teaches you how to embed YouTube video on Facebook. There are 2 high-level steps discussed in this guide:
How to Cancel Amazon Music Unlimited

How to Cancel Amazon Music Unlimited (PC or Amazon Apps)

This Itechguide teaches you how to cancel Amazon music unlimited. It offers steps to cancel Amazon music unlimited from a PC, Amazon...
How to Change Default Font in Google Docs

How to Change Default Font in Google Docs

This Itechguide teaches you how to change default font in Google Docs. Here are the high-level steps to change default font in...
How to Print Google Slides with Notes

How to Print Google Slides with Notes

This Itechguide teaches you how to print Google Slides with notes.

ADVERTISEMENTS

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...

BEST OF ITECHGUIDES

Best War Movies on Netflix

The 10 Best War Movies to Watch on Netflix

There are several war movies on Netflix and it is certain that an excellent war film is always a thriller.
Outlook 365: Subscription, Installation and Set Up

Outlook 365: Subscription, Installation and Set Up

If you are reading this you are probably one of the many people asking a lot of question about outlook 365 -...
Microsoft Edge is not in group policy

How to to Make Microsoft Edge Policy Available in Group Policy

Introduction When I was writing How to Set Homepage in Microsoft Edge I realized that Microsoft Edge is not...
Hotmail email (outlook.live.com)

Hotmail Email (Outlook.com Email): Your Definitive Guide

Introduction I suppose the first question on your mind is, "what happened to Hotmail email?" This is because when...
Windows 10 1909 (19H2 Preview) Features

Windows 10 1909 (19H2 Preview) Features

Introduction The latest version of Windows 10 1909 (19H2 Preview) was released on September 5, 2019. This guide details...

ADVERTISEMENTS

RECENT POSTS

Best Spanish Movies on Netflix

Top 10 Best Spanish Movies on Netflix

Firstly, our review is to give you top-notch Spanish movies on Netflix that will captivate you. As a result...

Our 10 Best French Movies on Netflix

Craving to know the best French movies available on Netflix? You are in the right place. We have a...
Best Kids Movies on Netflix

The 10 Best Kids Movies Now on Netflix

Do you want know the best kids movies on Netflix? We have put together a top collection of amazing movies on Netflix.
How to Add Thumbnail to YouTube Video

How to Add Thumbnail to YouTube Video (New or Existing Video)

This Itechguide teaches you how to add thumbnail to YouTube video. The guide offers steps to add thumbnail to YouTube video for...
Best Sci-Fi Movies on Netflix

The 10 Best Sci-Fi Movies to Watch on Netflix

Want the very best of Sci-Fi movies on Netflix? We have a list that will thrill you. The films...

MUST READ

How to Edit a Post on Facebook

How to Edit a Post on Facebook (from a Smartphone and PC)

Introduction This guide demos how to edit a post on Facebook from a smartphone or a PC.
How to Add Administrator to Facebook Page

How to Add Administrator to Facebook Page

Introduction After creating a Facebook page you can assign page roles. There are different page roles you can assign...
How to Delete a Google Calendar

How to Delete a Google Calendar (from a Smartphone or PC)

Introduction This guide demos how to delete a Google calendar from a smartphone or a PC.
NTFS Permissions with Share Permissions

NTFS Permissions with Share Permissions Compared

Introduction Combing NTFS permissions with share Permissions increases folder security within a network. Every Windows Administrator should have a...
windows update cannot currently check for updates

How to Fix “Windows Update Cannot Currently Check for Updates” Error

Introduction So, here is the situation. You want to update Windows 10. Then you click "check for updates" but...

LATEST DEALS

Exclusive Student Offer_Save 10% on selected Surface devices

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