Using the PowerShell Pipeline

This tip was provided to our WServerNews newsletter by Ashley McGlone, a Premier Field Engineer at Microsoft.

Are you aware that PowerShell has been publicly available and in use now for about seven years? Doesn’t sound so “new” any more does it? If you are still waiting to learn PowerShell, then you should start immediately. This is a key skillset for every IT Pro to know. It will be a career-differentiator for you.

As you learn PowerShell you will find that the “pipeline” is one of its most powerful features. You can stitch multiple commands into a one-line masterpiece (that could take 50+ lines of VBScript). Pipelines use the vertical bar character ( | ) to send the output from the command on the left as the input for the command on the right. You can pipe as many cmdlets as you like back-to-back. Here is an example:

PS C:\> Get-GPO -All | Where-Object {$_.ModificationTime -gt (Get-Date).AddDays(-30)} | Backup-GPO -Path C:\GPOBackups

Let’s break it down:

  1. Get a list of all GPOs [PIPE]
  2. Filter for the ones that have been modified in the last 30 days [PIPE]
  3. Backup the filtered GPOs in the list

The important thing to remember here is to structure the commands in the correct order. For this I recommend the old-school computer science 101 adage: Input | Process | Output. Begin with your input. Pipe to one or more processing cmdlets. Then send your output to a file, the screen, etc. NEVER EVER put a Format-* cmdlet in the middle of the pipeline. That is like crossing the streams. It will trash the entire line. PowerShell people use the motto “Filter left, format right.”

Here are some cmdlet examples by these three categories:

  • Input – Get-*, Import-*
  • Process – Group-Object, Where-Object, Sort-Object, Select-Object
  • Output – Set-*, Export-*, Out-*, Format-*, Write-*, ConvertTo-*

Keep this concept in mind as you build your PowerShell one-liners: Input | Process | Output. It will make life easier, and you will be a PowerShell Pipeline rock star in no time. Enjoy!

For more assistance on this topic, open up the PowerShell console and type: “Help about_Pipelines”

The above tip was previously published in an issue of WServerNews, a weekly newsletter from TechGenix that focuses on the administration, management and security of the Windows Server platform in particular and cloud solutions in general. Subscribe to WServerNews today by going to http://www.wservernews.com/subscribe.htm and join almost 100,000 other IT professionals around the world who read our newsletter!

Mitch Tulloch is a twelve-time recipient of the Microsoft Most Valuable Professional (MVP) award and a widely recognized expert on Windows Server and cloud computing technologies.  Mitch is also Senior Editor of WServerNews. For more information about him see http://www.mtit.com.

 

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top