If you want to make the most of Windows PowerShell, you have to clearly understand how the PowerShell objects and the PowerShell pipeline work together. PowerShell offers several commands that help us filter and structure the output of one command and pipe it to another.
People also search for:
- The Where-Object is the most used filtering command in PowerShell.
- It helps us to pass specific objects that satisfy a certain condition.
- The Where-Object idea is similar to the where clause in SQL.
- The ? is the alias for Where-Object.
Let us get the list of processes in order to get the file explorer. We get the list of processes using the below script.
If we use the this the script with the where-object, we can filter using the process name as shown below.
Clearly, we get only the needed output.
The comparison operator used is equal-to operator. The list of all the necessary operators are given below.
The select object is used to return only a few specific properties of the object.
- The select object is analogous to the select query in SQL. It is used to return the necessary columns.
- The select is the alias for Select-Object.
- It returns only the columns that match the columns specified by the user.
Let us get only the process name and the ID using the select alias as shown below.
Another interesting feature of select is the ability to select the number of objects. As we can choose to return top 100 rows in SQL, we can choose to return top 100 objects in Powershell.
- Here we choose to return only the top 5 rows.
PowerShell select-string gives us the advantage of selecting data within the object.
- The select-string is used to select the result inside a file.
- sls is the alias for select-string command.
- It returns the full path of the file, the line in which the text is found, the line number in which the text is found.
The output of the above script is given below.
- We can make it print only the line number by piping the output to the select command.
In PowerShell, there are many ways to filter the output of a command. In this post, we learned about the where-select and select-string commandlet. If you would like to see more commands, let us know in the comments below.