Prof. Powershell

Variable Pipeline Planning

Leave it to the -OutVariable parameter to get you the results that can be reused later.

If you've been using PowerShell for a while, you've probably created some complex pipeline expressions. I'd say the pipeline is probably one of the reasons PowerShell shines as an administrative and automation tool. However, you may have also run into problems trying to re-use or access a variable from earlier in the pipeline. Here's a one-line example:

PS C:\> get-childitem $env:temp -recurse | where {-Not $_.PSIsContainer} | group-object extension | Sort Count,Name | format-table Count,Name -autosize

This works just fine but perhaps you'd also like to add a custom column in the table that shows a percentage based on the total files, found from Get-ChildItem. By the time Format-Table is run, the file objects are no longer in the pipeline.

The solution is to take advantage of the common parameter -OutVariable. Every cmdlet supports this parameter. In this example I need to use it with Where-Object:

PS C:\> get-childitem $env:temp | where {-Not $_.PSIsContainer} -OutVariable files

The parameter has an alias of -ov. When I run this command, I still get normal output but a new variable called $files is also created that stores the results. When you use the parameter just specify the name you want for the variable. You don't include the $ character.

By the way an expression like this sort of achieves the same effect but you don't see the results of the expression:

PS C:\> $files = get-childitem $env:temp | where {-Not $_.PSIsContainer}

For our purposes this doesn't help us really:

PS C:\> get-childitem $env:temp -recurse | where {-Not $_.PSIsContainer} -ov files | group-object extension | Sort Count,Name | format-table Count, Name, @{label="PercentOfTotal";Expression={"{0:p2}" -f ($_.count/$files.count)}} -autosize

By the time PowerShell gets to Format-Table, the current object in the pipeline is a Group object. That is referenced by $_. To get information about the files, such as the total count, I can access the $files variable I created with -OutVariable. I used the alias in this example. Thus I can create a custom table column called PercentOfTotal that returns a percentage based on the count divided by the total number of files.

So when you have a complex pipelined expression where you want to access objects from earlier in the pipeline, -Out Variable should be exactly what you need.

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 Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus

SharePoint Watch

Sign up for our newsletter.

I agree to this site's Privacy Policy.