Prof. Powershell

Inspecting the Pipeline's Output

Without a doubt the PowerShell pipeline is a terrific tool that enables so much functionality with so 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 to see what it'd 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 okay, 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 okay. 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 is 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 -- I don't really need them, since 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 right property names to use with Format-Table because I looked at $step3. OutVariable may not solve all your problems, but it can be a useful debugging tool for peeking into the pipeline.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular