PowerShell Where-Object in List

In this PowerShell tutorial, I will explain how to use Where-object in a list in PowerShell. We will see various examples related to “PowerShell Where-Object in List“.

In PowerShell, the Where-Object cmdlet is used to filter elements in a list based on specified conditions. For example, to find all items in a list of numbers that are greater than 10, you would use $numbers | Where-Object { $_ -gt 10 }. This cmdlet allows for complex filtering by supporting script blocks with conditions that include comparison operators like -eq, -gt, -like, and others.

PowerShell Where-Object in List

The Where-Object cmdlet is part of PowerShell’s core cmdlets and is used to select objects with particular property values from the collection of objects passed to it. This cmdlet is commonly used in a pipeline, where it filters the objects passed from the previous cmdlet based on a script block or a condition.

Here’s the basic syntax of the Where-Object cmdlet:

<Collection> | Where-Object { <Condition> }

The <Collection> represents the set of objects you’re working with, and the <Condition> is a script block that specifies the criteria an object must meet to be included in the output.

Filter Lists with Where-Object in PowerShell

When working with lists in PowerShell, Where-Object becomes essential for filtering elements. Let’s say you have a list of numbers, and you want to find all the numbers that are greater than 10. Here’s how you would use Where-Object to accomplish this:

$numbers = 1..20
$filteredNumbers = $numbers | Where-Object { $_ -gt 10 }
$filteredNumbers

In this example, $numbers is a list of numbers from 1 to 20. The Where-Object cmdlet filters this list, and only numbers greater than 10 are stored in $filteredNumbers.

After you execute the code, you can see the output in the screenshot below.

PowerShell Where-Object in List

Examples of PowerShell Where-Object with List

Example 1: Filtering Strings

Imagine you have a list of file names, and you want to find files that have the “.txt” extension. Here’s how you can use Where-Object to filter those file names:

$fileNames = @('report.txt', 'summary.pdf', 'log.txt', 'data.csv')
$textFiles = $fileNames | Where-Object { $_ -like '*.txt' }
$textFiles

The -like operator is used for pattern matching, and the * wildcard character matches any string of characters. Therefore, only file names ending with “.txt” will be included in $textFiles.

Example 2: Filtering Custom Objects

Suppose you have a list of custom objects representing employees, and you want to filter out employees from a specific department. Here’s an example using Where-Object:

$employees = @(
    @{Name='John'; Department='Sales'},
    @{Name='Jane'; Department='Marketing'},
    @{Name='Dave'; Department='Sales'},
    @{Name='Anna'; Department='HR'}
)

$salesEmployees = $employees | Where-Object { $_.Department -eq 'Sales' }
$salesEmployees

In this case, $employees is an array of hashtables, each representing an employee. Where-Object filters the employees whose Department property equals “Sales”.

Here is the output in the screenshot below:

Examples of PowerShell Where-Object with List

Example 3: Combining Conditions

You can also combine multiple conditions using logical operators like -and, -or, and -not. Let’s filter a list of processes to find those that are consuming more than 50 MB of memory and are not named “svchost”:

$processes = Get-Process
$filteredProcesses = $processes | Where-Object { $_.WS -gt 50MB -and $_.Name -ne 'svchost' }
$filteredProcesses

Here, Get-Process retrieves a list of all running processes. The Where-Object cmdlet filters this list based on the conditions specified in the script block.

Conclusion

In this PowerShell tutorial, we discussed everything about PowerShell Where-Object in List with different examples.

You may also like: