Tips and Tricks

Prof. Powershell: Get Smart and Get -Out

PowerShell cmdlets come well-stocked with common parameters. Let's look at what you can get out of the -OutVariable parameter.

Just about all PowerShell cmdlets support a set of common parameters. These parameters are the same across all cmdlets regardless of the cmdlet's primary parameters -- which is to say the ones you use the most, like -computername. One common parameter you might want to use is -outvariable.

Usually you save a cmdlet's output to a variable like this:

PS C:\> $svc = get-service
This is perfectly acceptable because you likely plan on using $svc later. But you could also create $svc this way using -outvariable:
PS C:\> get-service -outvariable svc 

The only difference is this expression will also write the results to the console. But you'll still have a $svc object. Notice that the name of the variable doesn't need the $. This is because the parameter's value is a variable name and not an object. Here's another way you might use it:

PS C:\> get-service -outvariable svc | where 
	{$_.status -eq "stopped"} -outvariable stopped | 
    Sort Displayname | Select DisplayName
PS C:\> $stopped.count
PS C:\> $svc.count

This one-line PowerShell expression will produce a sorted list of all stopped services' displaynames, with some added benefits. The results of Get-Service have been saved to $svc. The filtered results from the Where-Object expression have been saved to variable $stopped. Now you can reuse the filtered services variable to work with just stopped services, without having to rerun or retype a long PowerShell command, like this:

PS C:\> $svc | export-
clixml stoppedservices.xml
This is an especially useful technique for long-running commands, where you need information parsed in several ways but don't want to have to rerun a command. Or where the original objects will be lost but you might still need them:
PS C:\> dir d:\files -recurse -outvariable myfiles | 
measure-object length -sum -maximum | 
Select Count,Sum,Maximum

If you want to do more with the files all you have to do is use the $myfiles variable. There's no need to rerun the DIR command, unless you want an updated list:

PS C:\> $myfiles | sort extension | 
   group extension | sort Count -descending | format-table Count,
   Name -autosize
PS C:\> ($myfiles | where {$_.extension -match ".doc"} -outvariable 
   docfiles | measure-object length -sum).sum2809966
PS C:\> $docfiles | Select Fullname

Parameters are all about efficiency. Look at the help file about_CommonParameters for more information.

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.