|
|||||||||||||||||
|
|
Usually you save a cmdlet's output to a variable like this: PS C:\> $svc = get-serviceThis 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
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.xmlThis 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. Jeffery Hicks, MCSE, MCSA, is a Microsoft PowerShell MVP and scripting guru for SAPIEN Technologies. Jeff is a 16 year IT veteran who has co-authored and written several books, courseware and training videos on administrative scripting and automation. He is the co-author of Advanced VBScript for Microsoft Windows Administrators (Microsoft Press) and Windows PowerShell:TFM (SAPIEN Press). His latest is WSH and VBSCript Core:TFM (SAPIEN Press). You can contact Jeffery about "Prof. Powershell: Get Smart and Get -Out" at jhicks@sapien.com.
|
||||||||||||||||
|
TOP Sponsored Links |
|||||||||||||||||
|
|||||||||||||||||