PowerShell foreach where-object example

Do you want to iterate over collections and filter items based on certain conditions in PowerShell? In this PowerShell tutorial, I will explain how to use where-object in foreach in PowerShell. Finally, I will show you some examples related to “PowerShell foreach where-object“.

To iterate over a collection and filter items in PowerShell, you can use the ForEach loop or ForEach-Object cmdlet to loop through each item and the Where-Object cmdlet to filter based on specific conditions. For example, to find and display all files modified in the last 7 days, you can use:

Get-ChildItem -Path C:\ -Recurse | Where-Object { $_.LastWriteTime -gt (Get-Date).AddDays(-7) } | ForEach-Object { Write-Host $_.FullName }

This pipeline lists files from the C: drive, filters them by modification date, and prints their full names.

PowerShell foreach where-object

The ForEach statement is used to iterate through a collection of objects in PowerShell. It allows you to execute a block of statements for each item in an array or a collection. Here’s a simple example:

$numbers = 1..5 # Creates an array of numbers 1 through 5
ForEach ($number in $numbers) {
    Write-Host "Number is: $number"

This script outputs each number in the $numbers array to the console.

ForEach-Object is the cmdlet equivalent of the ForEach statement, which allows you to perform actions on each item in a pipeline. Here’s an example of ForEach-Object in PowerShell:

$numbers = 1..5
$numbers | ForEach-Object {
    Write-Host "Number is: $_"

In this example, $_ represents the current object in the pipeline, which in this case is each number being piped from the $numbers array.

ForEach with Where-Object in PowerShell

PowerShell Where-Object is a cmdlet that selects objects from a collection based on their property values. When you need to filter items before iterating over them, you can combine Where-Object with ForEach or ForEach-Object. Here’s how you can use Where-Object within a ForEach loop:

$numbers = 1..5
$numbers | Where-Object { $_ -gt 2 } | ForEach-Object {
    Write-Host "Number is greater than 2: $_"

In this pipeline, Where-Object filters out the numbers that are greater than 2, and ForEach-Object then iterates over the filtered collection.

After executing the PowerShell script using VS code, you can see the output in the screenshot below.

PowerShell foreach where-object

PowerShell foreach where-object Examples

Let’s go through some practical examples to demonstrate the power of ForEach and Where-Object in real-world scenarios.

Example 1: Filtering Processes

Suppose you want to list all instances of Notepad running on your system. You can use Where-Object to filter these processes:

Get-Process | Where-Object { $_.ProcessName -eq 'notepad' } | ForEach-Object {
    Write-Host "Notepad Process ID: $($_.Id)"

This command gets all processes, filters for Notepad, and then outputs the process ID for each instance.

You can see the output in the screenshot below:

PowerShell foreach where-object Examples

Example 2: Searching for Files

Here is another example of PowerShell foreach where-object. Finding files that have been modified in the last 7 days could be another practical use:

Get-ChildItem -Path C:\ -Recurse | Where-Object { $_.LastWriteTime -gt (Get-Date).AddDays(-7) } | ForEach-Object {
    Write-Host "File: $($_.FullName) was modified in the last 7 days"

This command searches through the C: drive for files modified in the last week and outputs their full path.

Example 3: Active Directory Cleanup

If you’re an administrator needing to disable user accounts that haven’t been used in over 90 days, you could run:

Import-Module ActiveDirectory
Get-ADUser -Filter * -Properties LastLogonDate | Where-Object { $_.LastLogonDate -lt (Get-Date).AddDays(-90) } | ForEach-Object {
    Disable-ADAccount -Identity $_.SamAccountName
    Write-Host "Disabled account: $($_.SamAccountName)"

This snippet fetches all user accounts, filters out those not logged in within the last 90 days, and disables them.

Example 4: Log Parsing

This is the final example of PowerShell foreach where-object. You might need to parse through a log file to find error entries:

Get-Content -Path 'C:\Logs\example.log' | Where-Object { $_ -match 'ERROR' } | ForEach-Object {
    Write-Host "Error found: $_"

This command reads a log file and prints out each line that contains the word ‘ERROR’.


ForEach and Where-Object cmdlets in PowerShell is used for iterating over collections and filtering data. In this PowerShell tutorial, I have explained how to use where-object in PowerShell foreach with examples.

You may also like: