in

WindowsWindows Loving ItLoving It

[LATEST] PowerShell Filter to get appropriate objects in the results

Use Powerful PowerShell filters to get the appropriate objects in the results. where-object, select-object and more. – TecKangaroo PowerShell Filters

PowerShell filters

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.

Jump to:

People also search for:

Where-Object PowerShell:

  • 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.

gps

PowerShell get process

If we use the this the script with the where-object, we can filter using the process name as shown below.

gps | ? {$_.name -eq “explorer”}

Clearly, we get only the needed output.

get process explorer

The comparison operator used is equal-to operator.  The list of all the necessary operators are given below.

-ne === not equal to
-lt === less than
-le === less than or equal to
-gt === greater than
-ge === greater than or equal to
-like === like—a wildcard comparison
-notlike === not like—a wildcard comparison
-contains === contains the specified value
-notcontains === doesn’t contain the specified value

Select-Object PowerShell:

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.

gps | select name,id

get process name and id

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.

gps | select name,id -First 5

get top 5 using powershell filter

Select-String PowerShell:

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.

sls -Path “E:\96\documents\getalias.txt” -Pattern ‘alias’

The output of the above script is given below.

search for string
search for string
  • We can make it print only the line number by piping the output to the select command.

sls -Path “E:\96\documents\getalias.txt” -Pattern ‘alias’ | select LineNumber

search string and get line number
search string and get line number

Verdict:

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.

Leave a Reply

Your email address will not be published. Required fields are marked *