Getting Started with PowerShell: The Pipeline
Summary: Microsoft Scripting Guy, Ed Wilson, shares an excerpt from his new Windows PowerShell book.
Microsoft Scripting Guy, Ed Wilson, is here. Today, I have an excerpt from my recently published book, Windows PowerShell 3.0 First Steps. This book is published by Microsoft Press.
The Windows PowerShell pipeline takes the output from one command, and sends it as input to another command. By using the pipeline, you are able to do things such as find all computers in one specific location and restart them. This entails two commands:
- Find all the computers in a specific location
- Restart each of the computers
Passing the objects from one command to a new command makes Windows PowerShell easy to use inside the console because you do not have to stop to parse the output from the first command before taking action with a second command.
Windows PowerShell passes objects down the pipeline. This is one way that Windows PowerShell becomes very efficient: It takes an object (or group of objects) from the results of running one command, and it passes those objects to the input of another command.
By using the Windows PowerShell pipeline, it is not necessary to store the results of one command into a variable, and then call a method on that object to perform an action. For example, the following command disables all network adapters on my Windows 8 laptop.
Note Windows PowerShell honors the Windows security policy. Therefore, to disable a network adapter, you must run Windows PowerShell with Admin rights.
Get-NetAdapter | Disable-NetAdapter
In addition to disabling all network adapters, you can enable them. To do this, use the Get-NetAdapter cmdlet and pipe the results to the Enable-NetAdapter cmdlet, as shown here:
Get-NetAdapter | Enable-NetAdapter
If you want to start all of the virtual machines on a computer running Windows 8 (or on a server running Windows Server 2012), use the Get-VM cmdlet and pipe the resulting virtual machine objects to the Start-VM cmdlet:
Get-VM | Start-VM
To shut down all of the virtual machines, use the Get-VM cmdlet and pipe the resulting virtual machine objects to the Stop-VM cmdlet:
Get-VM | Stop-VM
In each of the previous commands, an object (or group of objects) that results from one command is piped to another cmdlet for further action.
Sorting output from a cmdlet
The Get-Process cmdlet generates a nice table view of process information to the Windows PowerShell console. The default view appears in ascending alphabetical order by process name. This view is useful for helping find specific process information; but it hides important details, such as which process uses the least, or the most, virtual memory.
To sort the output from the process table, pipe the results from the Get-Process cmdlet to the Sort-Object cmdlet and supply the –Property parameter to the property upon which to sort. The default sort order is ascending (that is, the smallest number appears at the top of the list).
The following command sorts the process output by the amount of virtual memory that is used by each process. The processes that consume the least amount of virtual memory will appear at the top of the list.
Get-Process | Sort-Object -Property VM
If you are interested in which processes consume the most virtual memory, you may want to reverse the default sort order. To do this, use the –Descending switch parameter. This command is shown here:
Get-Process | Sort-Object -Property VM –Descending
The command to produce the sorted list of virtual memory processes, and the associated output from the command are shown in the following image:
It is possible to shorten the length of Windows PowerShell commands that use the Sort-Object cmdlet. The command Sort is an alias for the Sort-Object cmdlet. A cmdlet alias is a shortened form of the cmdlet name that Windows PowerShell recognizes as a substitute for the complete cmdlet name. Some aliases are easily recognizable (such as Sort for Sort-Object or Select for Select-Object). Other aliases must be learned (such as ? for the Where-Object—most Windows users expect ? to be an alias for the Get-Help cmdlet).
In addition to using an alias for the Sort-Object cmdlet name, the –Property parameter is the default parameter that the cmdlet utilizes; therefore, it can be left out of the command. The following command uses the shortened syntax to produce a list of services by status.
Get-Service | sort status
It is possible to sort on more than one property. You need to be careful doing this because at times it is not possible to sort additional properties. With the Service cmdlets, a multiple sort makes sense because there are two broad categories of status: Running and Stopped. It therefore makes sense to attempt to organize the output further to facilitate finding particular stopped or running services.
One way to facilitate finding services is to alphabetically sort the DisplayName property of each service. The following script sorts the service objects obtained via the Get-Service cmdlet by the status, and then by the display name from within the status. The output appears in descending order instead of the default ascending list order.
Get-Service | sort status, displayname –Descending
The following image shows the command to sort services by status and display name and the output from the command:
Join me tomorrow when I will have another excerpt from my Windows PowerShell 3.0 First Steps book.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at email@example.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy