Prof. Powershell

Inspecting the Pipeline's Output

The OutVariable parameter offers a peek deep into your pipelined PowerShell expressions.

Without a doubt, the PowerShell pipeline is a terrific tool that enables high functionality with little effort. But sometimes things go wrong, and your PowerShell expression may not give you the results you expect. It would be helpful in these situations to peek into the pipeline and see what it's doing. Here's one technique you might use.

First, here's a PowerShell expression that doesn't do what I want:

PS C:\> Get-wmiobject win32_service -filter "state = 'running'" | sort StartAccount | foreach { $_| group-object -Property startAccount}

I'd like to get a count of all services running for each different service account on my computer, and this doesn't cut it. I need to see where things are breaking down in the pipeline as it runs without error. What I'll do is save the output of each pipeline part using the common parameter OutVariable, which has an alias of ov:

PS C:\> Get-wmiobject win32_service -filter "state = 'running'" -ov step1 | sort StartAccount -ov step2| foreach { $_| group-object -Property startAccount -step3}

Looking at $step1, I see all the running service objects, so I know the first part is working. The data in $step2 looks OK, but I don't see the start account name. Let me try this:

PS C:\> $step2 | Select Name,StartAccount

Oops. StartAccount is blank. Maybe that's the wrong property. Let me look at a service object's properties:

PS C:\> $step1[0] | Select *

Now I see. The property I want is Startname. I modify my expression and try again. Still not right. $step1 and $step2 are OK. Viewing $step3, I can see that I'm not getting what I expected. Only one item is in $step3.

After a little thought -- and experience -- I realize using ForEach is the wrong approach. It's piping each item one at a time to Group-Object. I can even look at $step2 to see what objects it's processing. Instead, I simply need to pipe directly to Group-Object:

PS C:\> get-wmiobject win32_service -filter "state = 'running'" -ov step1 | sort Startname -ov step2 | group-object -Property startname -ov step3

Finally, I see the output I was expecting. I can even polish it up a bit and remove the OutVariable parameters, even though I don't really need them because it's now running fine:

PS C:\> get-wmiobject win32_service -filter "state = 'running'" | sort Startname | group-object -Property startname |format-table Name,Count -auto

I know those are the corrrect property names to use with Format-Table because I looked at $step3. OutVariable may not solve all your problems, but it can function as a useful debugging tool for peeking into the pipeline.

About the Author

Jeffery Hicks is a Microsoft MVP in Windows PowerShell, Microsoft Certified Trainer and an IT veteran with over 20 years of experience, much of it spent as an IT consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He works today as an independent author, trainer and consultant. Jeff writes the popular Prof. PowerShell column for and is a regular contributor to the Petri IT Knowledgebase and 4SysOps. If he isn't writing, then he's most likely recording training videos for companies like TrainSignal or hanging out in the forums at Jeff's latest books are Learn PowerShell 3 in a Month of Lunches, Learn PowerShell Toolmaking in a Month of Lunches and PowerShell in Depth: An Administrators Guide. You can keep up with Jeff at his blog, on Twitter at and on Google Plus (http:/

comments powered by Disqus

Reader Comments:

Add Your Comment Now:

Your Name:(optional)
Your Email:(optional)
Your Location:(optional)
Please type the letters/numbers you see above

Redmond Tech Watch

Sign up for our newsletter.

I agree to this site's Privacy Policy.