Behind the PowerShell Pipeline logo

Behind the PowerShell Pipeline

Subscribe
Archives
March 22, 2022

Creating Better PowerShell Output Part 2

Last time, I started to explain good construction techniques for your PowerShell code. We’re going to continue this topic by looking at what should be a simple concept: Functions should write one type of object to the pipeline. That sounds rather simple, but you’d be surprised by the number of scripters who fail to heed this advice. I want to show you why this is important and a few scripting techniques you can use to get more from your PowerShell functions.

Don’t Mix Your Output

If your PowerShell function is writing output to the pipeline, it should only write one object type. You can write as many objects as you want, but they should all be of the same type. One way you can ensure this is by not writing monolithic functions. A PowerShell function should be relatively small and do one thing. This is the way PowerShell cmdlets are designed. We have one command that gets a service and separate command that sets a service. We don’t have one command that does both. Your functions show follow this principle.

Want to read the full issue?
GitHub Bluesky LinkedIn About Jeff
Powered by Buttondown, the easiest way to start and grow your newsletter.