In order to understand how powerful Windows PowerShell is, you need to look at the filtering capabilities it offers. With an understanding of objects and pipelining, you can retrieve the exact information you are looking for. The Usage of Pipeline section talks about how pipelines can be used to pass one or more objects, which are the output of one command, and feed them into another cmdlet. In Windows PowerShell, there are commands that will help you to refine and get the desired output. Some of these cmdlets that we will look in this section are Where-Object
, Sort-Object
, Select-Object
, and Select-String
.
The Where-Object
cmdlet helps you to filter the output returned by other cmdlets. For example, the Get-service
cmdlet without any parameter retrieves information about the services installed on the computer on which it is called:
Now, let's filter the output of Get-Service
to find the list of services that are stopped:
Windows PowerShell supports the following logical operators that we are going to use in our next example of filtering the output of a Get-Service
cmdlet:
Operator |
Description |
---|---|
| |
| |
|
This is |
| |
|
We will now find services that are running whose name starts with Windows using the –and
operator:
By default, Windows PowerShell will sort the output of commands using an alphabetical order. You can then use the Sort-object
cmdlet to sort it using any way you like. For example, the following example will first sort the output of the Get-Process
cmdlet by the Process ID and then the CPU time:
At times, you might need to filter your results based on certain specific property names. This is where the Select-Object
cmdlet comes in handy. It selects the stated properties of a single or group of objects. You can use Select-Object
to select objects that are unique in a group of objects It supports parameters such as First
, Last
, Skip
, and Index
. You can select one or multiple object properties using the –Property
parameter. Let's take an example of the output of the Get-Process
cmdlet, and let's say you are troubleshooting a performance issue on a server and are only interested to know about the CPU time and process name properties. The output of the Get-Process
cmdlet will be piped to Select-Object
with the –property
parameter set to the process name and CPU. The Format-list
cmdlet will format the output displayed in the console:
PS C:\> Get-Process | Select-Object -Property Processname, CPU | format-list
Now, let's use another parameter called –Unique
of Select-Object
. This will select a single member from a number or objects with same properties and values. In the following example, you will notice that the –Unique
parameter is case sensitive and reports "c" and "C" as unique.
Another useful filtering cmdlet is Select-String
, used to select text patterns in strings and files. If you are familiar with UNIX shells, it is used as Grep
or Findstr
in Windows. It can display all the matches or stop after the first match depending on the usage of the cmdlet. You can search files of Unicode text using this command as well.
First, we will create a file using the redirection operator (>
) to store the list of services running on a server to a text file:
PS C:\> Get-Service > service.txt
Now, we will find the text "Network" and "Windows" to get a list of all the services that have the word in it. We will then use the –notmatch
parameter to list all the services except this word:
Note that the search performed here is a case insensitive search; you can use the —case sensitive
parameter to make it case sensitive, which means you won't get any output if you use the parameter with the previous command as there are no matches found.