In PowerShell, both -Filter and Where-Object (where) are used to filter objects, but they serve different purposes and have different performance implications. Here’s a comparison to help you understand when to use each:
​
-Filter
-
Usage: -Filter is used with cmdlets like Get-ChildItem and Get-WmiObject to filter objects at the source.
-
Performance: Since -Filter performs the filtering at the source (like the file system or a WMI provider), it is generally much faster and more efficient.
​
Where-Object
-
Usage: Where-Object (where) is a generic cmdlet that can filter objects based on any condition. It is used after the objects have been retrieved.
-
Performance: Where-Object can be slower compared to -Filter because it processes all objects in the pipeline after they have been retrieved.
​
When to Use Which
-
Use -Filter: When you are able to use it with cmdlets that support it, as it improves performance by reducing the number of objects that need to be processed.
-
Use Where-Object: When -Filter is not available or when you need to filter based on more complex conditions that -Filter cannot handle.
​
Summary
-
-Filter: Use when supported by the cmdlet for better performance.
-
Where-Object: Use for more complex conditions or when -Filter is not available.
Page Menu
ForEach
ForEach is a language keyword that allows you to iterate over a collection of items. It is typically used in a script or a command block where you need to perform an operation on each item in a collection.
Standard Nested Syntax \ Structure
ForEach ($item in $collection) {
# Perform actions on $item
}
Code Example | Loop Through Array of Data
# Array of strings
$fruits = @("Apple", "Banana", "Cherry")
# Using ForEach to iterate over each fruit
ForEach ($fruit in $fruits) {
Write-Output "Fruit: $fruit"
}
Output
Fruit: Apple
Fruit: Banana
Fruit: Cherry
Page Menu
Key Points
-
Scope: The ForEach keyword iterates over a collection in a foreach loop structure.
-
Context: It is typically used in scripts or command blocks where a loop structure is needed.
-
Performance: It is generally faster than ForEach-Object for looping through large collections because it operates at a lower level.
Inline Strucure
ForEach ($item in $collection) {$item} # Perform actions on
Inline Strucure
# Array of strings
$fruits = @("Apple", "Banana", "Cherry")
# Using ForEach to iterate over each fruit
ForEach ($fruit in $fruits) {Write-Output "Fruit: $fruit"}
Try it Yourself ?
# Import Sample CSV Sheet
$SampleData2 = Import-csv C:\temp\Sample-Data.csv
# Loop Through Each Record in CSV and Find Miami, Assign to Variable
$MiamiUsers2=Foreach($Sample2 in $SampleData2){
# Conidtional Statement Check City
If($Sample2.City -eq "Miami"){
# Return Staff Name From Miami
$Sample2.'Full Name'
}
}
# Sort Names, Select First 10 Names
$MiamiUsers2 | Sort-Object | Select -First 10
ForEach-Object
ForEach-Object is a cmdlet designed to process each item as it passes through the pipeline. This is particularly useful in one-liners or when working with pipelines.
Page Menu
Standard Nested Syntax \ Structure
$collection | ForEach-Object {
param($item)
# Perform actions on $item
}
Nested Structure | Loop Through Array of Data
# Array of strings
$fruits = @("Apple", "Banana", "Cherry")
# Using ForEach-Object to iterate over each fruit
$fruits | ForEach-Object {
Write-Output "Fruit: $_"
}
Output
Fruit: Apple
Fruit: Banana
Fruit: Cherry
Key Points
These examples demonstrate the basic usage of ForEach-Object in various contexts, from simple arrays and custom objects to filtering and modifying items in a collection. By understanding these basic patterns, you can leverage ForEach-Object to perform a wide range of operations in PowerShell pipelines.
Inline Strucure
$collection | ForEach-Object {param($item)} # Perform Action
Inline Strucure
# Array of strings
$fruits = @("Apple", "Banana", "Cherry")
# Using ForEach-Object to iterate over each fruit
$fruits | ForEach-Object {Write-Output "Fruit: $_"}
Try it Yourself ?
# Import Sample CSV Sheet
$SampleData1 = Import-csv C:\temp\Sample-Data.csv
# Loop Through Each Record in CSV and Find Miami & Assign to Variable
$MiamiUsers1=$SampleData1 | ForEach-Object {If($_.City -eq "Miami"){$_.'Full Name'}}
# Sort Names, Select First 10 Names
$MiamiUsers1 | Sort-Object | Select -First 10
Where-Object
Get-Process | Where-Object { $_.CPU -gt 100 }
In PowerShell, the Where-Object cmdlet (often aliased as ?) is used to filter objects based on specified conditions. This is particularly useful when dealing with collections of data, such as arrays or output from other cmdlets. Here are some examples and detailed information to help new users understand how to use the Where-Object cmdlet effectively.
​
Basic Usage
The Where-Object cmdlet filters objects passed through the pipeline based on a script block that evaluates to $true or $false. Here's a simple example:
​
​
​
​
This command retrieves all processes and filters them to only
show those where the CPU property is greater than 100.
Using the Alias ?
A shorthand for Where-Object, making it quicker to type but functionally identical.
The previous example using the alias would be:
​
​
​
​
​
​
​
Filtering with Comparison Operators
You can use various comparison operators within the script block to filter data:
-
-eq: Equal to
-
-ne: Not equal to
-
-gt: Greater than
-
-lt: Less than
-
-ge: Greater than or equal to
-
-le: Less than or equal to
Get-Process | ? { $_.CPU -gt 100 }
Page Menu
Examples:
Filter by Size
Get-ChildItem -Path C:\Temp | Where-Object { $_.Length -gt 1MB }
Filter Where Services Are Running
Get-Service | Where-Object { $_.Status -eq 'Running' }
Using Logical Operators
Logical operators can be combined within
the script block to create more complex conditions:
-
-and: Logical AND
-
-or: Logical OR
-
-not: Logical NOT
Filter Process by Multiple Conditions
Get-Process | Where-Object { $_.CPU -gt 100 -and $_.WorkingSet -gt 1GB }
Filter Files by Extension and Size
Get-ChildItem -Path C:\Temp | Where-Object { $_.Extension -eq '.txt' -or $_.Extension -eq '.log' }
More Advanced Scriptblocks
$threshold = 100MB
Get-ChildItem -Path C:\Temp | Where-Object {
$file = $_
$file.Length -gt $threshold -and $file.LastWriteTime -gt (Get-Date).AddDays(-30)
}
Summary
-
Basic syntax: Where-Object { <condition> } or using alias ? { <condition> }
-
Comparison operators: -eq, -ne, -gt, -lt, -ge, -le
-
Logical operators: -and, -or, -not
-
String matching: -match (regex), -like (wildcard)
​
By understanding and using these examples, you can effectively filter objects in PowerShell to suit your needs. The Where-Object cmdlet is a powerful tool for querying and manipulating data in scripts and command-line operations.
Example: Gathers information, Where-Object for filtering; the $_. allows object oriented property selection of "Name"; then uses a comparison operator "-like" against a string with a wildcard *
#==================
# Where Clause Syntax
#==================
Get-Service | Where-Object{$_.Name -like "We*"} | Select Name, Status
Get-Service | Where{$_.Name -like "We*"} | Select Name, Status
Get-Service | ?{$_.Name -like "We*"} | Select Name, Status
-Filter
​
Filtering is a way to get a subset of the data avaialbe, so you only get informaiton you require.
# Filter with Active Directory Cmdlet Get-ADuser
Get-ADuser -Filter {(name -like "Ste*") -and (Enabled -eq $True)} | Select Name, SamAccountName
​
​
​
​
# Filter with Active Directory Cmdlet Get-ADcomputer
Get-ADCOmputer -Filter {(name -like "My*") -and (Enabled -eq $True)} | Select Name
Page Menu
Cmdlets that Support -filter
Special Note: The -Filter Parameter is not avilable on all Cmdlets. Example would be Get-Service