Prof. Powershell

Out, Out Darn Variable

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 that 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
86
PS C:\> $svc.count
159

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 I want to do more with the files all I have to do is use the $myfiles variable. There's no need to rerun the DIR command, unless I 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).sum
2809966
PS C:\> $docfiles | Select Fullname

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

comments powered by Disqus
Most   Popular