Skip to the main content.

Unlocking the Power of PowerShell: Tips for Success

Embrace the PowerShell pipeline

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 

 

That’s because PowerShell’s pipeline operator (|) is closely tied to script blocks ({}). 
Here’s 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. Let’s illustrate this with an example.

Here’s 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. Let’s 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. What’s better, the function now runs about 10 times faster than ForEach-Object or Where-Object.

To make the function truly reusable, you’d 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 

 

 

 

Good2know

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.

PowerShell Poster 2023

Get your poster here!

 

 

Related links 

 

Related posts

4 min read

Understanding PowerShell Arrays

PowerShell arrays are a powerful and versatile way of managing collections of data, enabling you to efficiently...

5 min read

Enhance PowerShell scripts to validate and transform data

Tired of hidden errors in your PowerShell scripts? Discover how validation and transformation attributes can bring...

4 min read

Embrace the PowerShell pipeline

Want to make your PowerShell scripts faster and more flexible? Learn how to optimize the pipeline with script blocks...

About the author: