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 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.


  • Microsoft Bolsters Windows IoT with NXP and SQL Server Support

    Microsoft's Internet of Things (IoT) product line is continuing to grow, with a few new developments highlighted this week.

  • Tamper Protection Now Available to Microsoft Defender ATP Subscribers

    The Microsoft Defender Advanced Threat Protection (ATP) E5 subscription plan now has an optional "tamper protection" security feature, Microsoft announced on Monday.

  • Exploring OCR, a New Way To Get Data into Excel

    Microsoft recently added a new optical character recognition feature to Excel that lets users import data from a photograph taken from a smartphone. Here's how to use it.

  • Microsoft Authenticator App To Get Real-Time Phishing Protections

    Microsoft is working on adding capabilities to its Microsoft Authenticator app to help defeat security breaches enabled by advanced attack techniques, including phishing and man-in-the-middle methods.

comments powered by Disqus

Office 365 Watch

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.