Summary: Microsoft Scripting Guy, Ed Wilson, shares a portion from his popular Microsoft Press book Windows PowerShell 3.0 First Steps.
Microsoft Scripting Guy, Ed Wilson, is here. Today I want to share with you another portion from my new book, Windows PowerShell 3.0 First Steps, which was recently released by Microsoft Press.
To read the previous parts of this series, see:
Introduction to the pipeline
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 like find all computers in one specific location and restart them. There are two commands in this request:
- 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 and 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.
Get-NetAdapter | Disable-NetAdapter
Note Windows PowerShell honors the Windows security policy. Therefore, to disable a network adapter, Windows PowerShell must run with Admin credentials. For more information about starting Windows PowerShell with Admin credentials, refer to Chapter 1 in Windows PowerShell 3.0 First Steps.
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 in Windows 8 (or 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) resulting from one command pipes to another cmdlet for further action.
Sorting output from a cmdlet
The Get-Process cmdlet generates a nice table view of process information in the Windows PowerShell console. The default view appears in ascending alphabetical process name order. This view is useful to help 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 on which to sort to the –Property parameter. The default sort order is ascending (that is smallest numbers appear 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 consumes the least amount of virtual memory 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 as shown here:
Get-Process | Sort-Object -Property VM –Descending
The command to produce the sorted list of processes for the virtual memory, and the associated output from the command are shown in the image that follows.
It is possible to shorten the length of Windows PowerShell commands that use the Sort-Object cmdlet. The Sort command 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. For services, a multiple sort makes sense because there are two broad categories of status: Running and Stopped. It 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 sort alphabetically the DisplayName property of each service. The script that follows sorts the service objects that are obtained via the Get-Service cmdlet by the status, and then by DisplayName from within the status. The output appears in descending fashion instead of the default ascending sorted listing.
Get-Service | sort status, displayname –Descending
The command to sort services by Status and DisplayName, and the output from the command are shown in the following image.
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 firstname.lastname@example.org, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy