PowerShell: Where-Object vs Select-Object

Are you confused and unable to know whether you should use the Where-Object or the Select-Object in PowerShell? In this PowerShell tutorial, I will explain the differences between PowerShell where-object vs. select-object and when you should use which one.

What is Where-Object in PowerShell?

The Where-Object cmdlet in PowerShell is used to filter objects based on their property values. It allows you to pass through objects that meet certain criteria and exclude those that do not. Essentially, Where-Object is a filtering tool.

For example, if you want to find processes that are consuming more than 50MB of memory, you could use Where-Object like this:

Get-Process | Where-Object { $_.WS -gt 50MB }

In this command, Get-Process retrieves all processes, and Where-Object filters these processes based on the condition provided in the script block { $_.WS -gt 50MB }, where $_ represents each object (process) in the pipeline, .WS refers to the working set (memory usage), and -gt is the operator for ‘greater than’.

What is Select-Object in PowerShell?

On the other hand, Select-Object is used to select specific properties of an object or a set of objects. It can also be used to create new calculated properties or to select a unique or specific number of objects from a collection.

For instance, if you want to display only the name and ID of all processes, you could use Select-Object like this:

Get-Process | Select-Object Name, Id

This command will output a table with just the Name and Id properties of each process.

Examples of Where-Object and Select-Object in Use

Let’s consider a real-world scenario where you might need to use both Where-Object and Select-Object in PowerShell. Suppose you need to retrieve a list of currently running services and display their name and status. Here’s how you could do this by using the below PowerShell script.

Get-Service | Where-Object { $_.Status -eq 'Running' } | Select-Object Name, Status

In this pipeline, Get-Service retrieves all services, Where-Object filters only the services that are running, and Select-Object selects the Name and Status properties to be displayed.

Differences Between PowerShell Where-Object and Select-Object

Now, let’s summarize the differences between these two cmdlets: PowerShell Where-Object and Select-Object.

PowerShell Where-Object vs Select-Object
FeatureWhere-ObjectSelect-Object
PurposeFilters objects based on property valuesSelects specific properties of objects
UsageTo include/exclude objects in the pipelineTo manipulate or display object properties
ExampleGet-Process | Where-Object { $_.CPU -gt 10 }Get-Process | Select-Object Name, CPU
OutputObjects that meet the criteriaObjects with only the selected properties
FlexibilityUsed for comparison operationsUsed for property selection and manipulation

Conclusion

Where-Object and Select-Object are two distinct cmdlets in PowerShell with different purposes. Where-Object is a filter that allows you to specify conditions to include or exclude objects in the pipeline, whereas Select-Object is used to select specific properties from objects or to create new calculated properties.

I hope now you know the differences between PowerShell Where-Object and Select-Object cmdlets and you know where to use Where-Object and where to use the Select-Object in PowerShell.

You may also like: