Filtering and using Comparison operators in PowerShell

Time for a new post!

This time I figured I could do a write-up on how to filter data in PowerShell and make it display the information you want to see.

Please take note that everything in this post is done with PowerShell.exe, and some examples might not work in ISE or VSCode.

In PowerShell there are two main ways to filter out data:

  1. Using a filter parameter in your first cmdlet (your command)
  2. Using other cmdlets to filter objects with comparison operators

If you don’t know what objects are in PowerShell, I recommend you check out my other post “What are Objects in PowerShell?”.

Before we start exploring these two options, I’m going to tell you about a “rule of thumb” that I learned from reading the book “Learning PowerShell in a month of lunches” :

Filter left first.

What this means is that you always want to try and filter/limit the output of your commands as far left in the command as possible. The further left in the command you are able to filter out data, the less resources you are going to use on whatever device you are running commands on. This could be a normal endpoint, or a server. When scripting you always want to use as little resources as possible.

Now exploring the two different ways of filtering commands, I’ll show you examples of filtering on the left side and using other cmdlets on the right.

Using a filter parameter in your first cmdlet

The reason I’m writing “a filter parameter” and not “the -filter parameter” is because the parameter in cmdlets for filtering data is not always the same.

Lets look at this command as our first example:

 Get-LocalGroup 

get-localgroup

Now say we want to filter our objects based on the Name property while “filtering left”.

We start by looking at all the parameters of the Get-LocalGroup cmdlet by running

 Get-Help Get-LocalGroup -Parameter * 

We see that Get-LocalGroup has a -Name parameter, and it accepts wildcards!

get-helpparameter

Now lets look back at our first Get-LocalGroup cmdlet. Say we want the Administrators group. We can use the -Name parameter we found to run:

 Get-LocalGroup -Name Administrators 

get-localgroupadmin

If we want to be less specific and just filter for anything that contains *admin* (wildcards) we can run this instead:

 Get-LocalGroup -Name *admin* 

get-localgroupadmin2

Now lets look at the other way to filter objects.

Using other cmdlets to filter objects with comparison operators

If your first cmdlet doesn’t provide with any parameter to filter out data, you can use another PowerShell cmdlet called Where-Object, but you’ll need to know how to use comparison operators in order to effectively filter out the data you want.

In short, you use comparison parameters inside the Where-Object cmdlet in order to compare data. The comparison always returns a Boolean value: True or False.

These are some of the comparison operators in PowerShell:

  • -eq (returns True when equal, returns False when not equal.)
    • Example 1: ‘string’ -eq ‘string’ (would return True)
    • Example 2: 13 -eq 37 (would return False)
  • -ne (returns True when not equal, returns False when equal.)
    • Example 1: ‘string’ -ne ‘hello’ (would return True)
    • Example 2: 3 -ne 3 (would return False)
  • -ge
    • Greater than or equal to
  • -le
    • Less than or equal to
  • -gt
    • Greater than
  • -lt
    • Less than

When comparing strings using these comparison operators, PowerShell does not care about upper or lower case strings. It’s all the same. If you want to be case sensitive, you can add a “c” in front of the operators. (For example -ceq or -cne.)

To read more about comparison operators and to see the full list, take a look at Get-Help about_Comparison_Operators. One of the operators I recommend reading about is -Like and -Contains.

Now let me show you some examples of using Where-Object and comparison operators to find the same Administrator group from before.

 Get-LocalGroup | Where-Object -FilterScript {$_.Name -eq 'Administrators'} 

whereobject1

I know that looks hard, so let me explain what happens:

  1. You run the Get-LocalGroup cmdlet and fetch ALL the local groups on the computer
  2. You send all the objects from that command through the pipeline into our new Where-Object cmdlet
  3. With Where-Object we use the -FilterScript parameter and add a {} script block
  4. Inside this {} script block, we pass our current object in. Then we use a comparison operator and we write our second object so we have something to compare our first object to.
  5. $_ is a reference to the current object in the pipeline (in this case all the localgroups).
  6. .Name references the Name property of the current object.
  7. Combined $_.Name references the name property of the local group objects.
  8. -eq does a “Equal to” comparison like we talked about above against the second object we threw into the {} script block which is a ‘Administrators’ string.
  9. Our command then spits out objects where the Name property is equal to our string.

Phew..

Now as a last example I want to use the -Like comparison operator in order to be less specific. (I wrote further up in the post where you can read about the -Like operator.)

 Get-LocalGroup | Where-Object -FilterScript {$_.Name -like "*admin*"} 

whereobject2.png

I won’t go trough how the command flows because it would be pretty much the same as the previous command. Except that we changed our comparison operator and we changed our second object to get the result we wanted.

I hope you found this post useful!

Please share any thoughts or questions down in the comments, I am still very new to writing these posts. I appreciate feedback.

// Tr4p

2 thoughts on “Filtering and using Comparison operators in PowerShell

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.