Want to make your PowerShell scripts faster and more flexible? Learn how to optimize the pipeline with script blocks and reusable functions!
Did you know that you can assign PowerShell control structures to variables, but you cannot pipe them through the PowerShell pipeline?
This works fine:
# this works
$result = for ($x=0; $x-lt100; $x+=3)
{
$x
}
$result
However, piping the results from a control structure to another command just fails:
# this fails
for ($x=0; $x-lt100; $x+=3)
{
$x
} | Out-GridView
Thats because PowerShells pipeline operator (|) is closely tied to script blocks ({}). Heres how you can stream-enable any control structure and add real-time processing to it:
# this works
. {
for ($x=0; $x-lt100; $x+=3)
{
$x
}
} | Out-GridView
All it takes is enclosing the control structure in a script block and calling it using the . operator (no new variable scope is created). With this knowledge, you can already use the superfast classic foreach or do loops inside a pipeline.
How pipeline works
The entire PowerShell pipeline concept is driven by script blocks. You can replace ForEach-Object or Where-Object with simple script blocks.
All three lines produce the same result:
Get-Service | ForEach-Object { "Working on $($_.DisplayName)..." }
Get-Service | ForEach-Object -Process { "Working on $($_.DisplayName)..." }
Get-Service | . { process { "Working on $($_.DisplayName)..." } }
Likewise, all three lines produce the same result:
Get-Service | Where-Object Status -eq Running
Get-Service | Where-Object { $_.Status -like 'Running' }
Get-Service | . { process { if ($_.Status -like 'Running') { $_ } } }
Why is that useful? Because you can now run ForEach-Object in its own variable scope if needed. Simply replace the . operator with &.
More importantly, you can easily turn ForEach-Object and Where-Object into standalone functions, allowing you to create reusable code. Lets illustrate this with an example.
Heres a "normal" pipeline approach that lists all files in the Windows folder that have been changed within the past 24 hours:
$now = Get-Date
$cutoff = $now.AddHours(-24)
Get-ChildItem -Path c:\Windows -File | Where-Object { $_.LastWriteTime -gt $cutoff }
It works well in this particular script; however, filtering files by 'age' is something that could be useful in many scripts. You may not always want to start from scratch. Lets see how the new script block knowledge can help turn ad-hoc commands like ForEach-Object and Where-Object into new and useful specialized commands.
When translated to script blocks, the previous code looks like this:
$now = Get-Date
$cutoff = $now.AddHours(-24)
Get-ChildItem -Path c:\Windows -File | & { process { if ($_.LastWriteTime -gt $cutoff) { $_ } } }
Any script block can easily be turned into a function by using the "function" keyword and giving it a name:
function Filter-NewFile
{ process { if ($_.LastWriteTime -gt $cutoff) { $_ } } }
$now = Get-Date
$cutoff = $now.AddHours(-24)
Get-ChildItem -Path c:\Windows -File | Filter-NewFile
At this point, the code is much more structured and easier to read. Whats better, the function now runs about 10 times faster than ForEach-Object or Where-Object.
To make the function truly reusable, youd only need to internalize the helper variables it requires. With just a few changes, the initial code has become a versatile, reusable command that can be useful in many future projects:
function Filter-NewFile
{
param
(
[int]$Hours
)
begin
{
$now = Get-Date
$cutoff = $now.AddHours(-$Hours)
}
process
{
if ($_.LastWriteTime -gt $cutoff)
{ $_ }
}
}
Get-ChildItem -Path c:\Windows -File | Filter-NewFile -Hours 480
Your ultimate PowerShell Cheat Sheet
Unleash the full potential of PowerShell with our handy poster. Whether you're a beginner or a seasoned pro, this cheat sheet is designed to be your go-to resource for the most important and commonly used cmdlets.The poster is available for download and in paper form.
Related links
- All of our goodies for you on one page: Active Directory, Graph, Teams, Exchange and the general Cheat Sheet and more on Security
- ScriptRunner ActionPacks will help you automate tasks
- Try out ScriptRunner here
- ScriptRunner: Book a demo with our product experts