Practicing The Pipeline

This week I have a short but accurate (you thought I was going to say sweet didn’t you?) article for you. Often in PowerShell you’ll find yourself wanting to chain together several commands, the pipeline will be your tool of choice. PowerShell turns the pipeline from the least used character on your keyboard into your best friend. Simply put the pipeline tells PowerShell to take the results from the right side of the pipeline and apply the action to the left pipeline.

A Few Perfect Pipelines

While there are many possible permutations, one of the most common uses of the pipeline (in my experience) looks like this…

(Sample GET command) | (Sample SET command)

In our first example we’ll use the pipeline to export all users in your tenant to a csv.

Get-MSOLUser | Export-CSV c:\PS\Users.csv

This one-liner will retrieve all users from your tenant and then take that output and export it to a CSV in C:\PS\users.csv. Now we’re going to kick it up a notch and chain together 3 commands!

Get-MsolUser | Where-Object {$_.FirstName -eq "John"} | Export-Csv c:\PS\Users.csv

The script above is the same as before but with a twist. We’ve spliced filter which will limit out Get-MsolUser command to only those named John, from there we have the3rd step which is to take our filtered results and export them to a CSV. If you’re looking for more information on the use of the Where-Object command then might I suggest this awesome article on filtering?

The Wrap Up

And there you have it a nice introduction to the pipeline. The pipeline in PowerShell will help simplify those 2 or 3 line scripts into tidy one-liners. As always, if you have any questions or suggestions for future topics please feel free to leave a comment below or shoot me an email using the contact for on the side of this page.

#powershell #ps101 #pipeline #csv

Featured Posts
Recent Posts
Search By Tags
No tags yet.