PowerShell 104 – Operators

For this PowerShell 104 we’re handling comparison operators. I love operators, I use them in pretty much every script I’ve written. They’re nice to use whenever you need to check something or make a decision on what to do next. They are priceless, I couldn’t go without them.

The simple operators

In it’s most simple form operators are the logic that enable you to compare stuff. So for example check if 1 equals 1:

Which it obviously does so it outputs back “True”. Whenever you compare something that isn’t equal you’ll get “False” back.

Similar to this comparison you can also use other operators:

You can use this in an identical way as  -eq .

It gets more fun with  -like It supports wildcards:

The “*” symbol stands for one or more wilcard characters. The “?” symbol stands for a single wildcard character.

The more complex operators

This is still pretty simple. The more complex stuff start at -match . It can do the same as  -like with some additional options. For example it supports regular expressions which is a whole different topic. I’ll cover that in a later blogpost. I’m not a big fan of  -match anyway. Regular expressions are complex and I’ve never had the need for  -match .

Next up, and more interesting, is -contains . It checks if a value is present in an array. For example:

 

Practical uses

The most important questions, how do you use these things? One of the most common uses is combining it with an if  statement. For example:

So you can use them for check something and then execute a piece of code only if it returns true. It’s a decision maker. Using this you can make decisions in scripts to perform one thing or another. For example you want to check on a machine which user profile are on there. On Windows XP (which I hope you don’t have anymore) you would need to check a different folder then Windows Vista and up. So based on Windows version you would need to check one folder or the other.

Another nice combo is with the  where-object cmdlet. Using this cmdlet in combination with a comparison operator enables you to select specific entries you get back from another cmdlet. For example:

Returns a list of services that are in a Stopped state.

It gets more fun though when you start to combine multiple comparisons like this:

This returns a list of services that are in Stopped state and are configured to start automatically.

 

There are more comparison operators out there but this post will give you a grasp about how you can use them. That’s it for this PowerShell course!

PowerShell 1XX post series

  1. PowerShell 101 – The absolute basics
  2. PowerShell 102 – Practice scripts
  3. PowerShell 103 – Pipelines
  4. PowerShell 104 – Comparison Operators (current)

3 thoughts on “PowerShell 104 – Operators

  1. Dennis Geerlings

    The biggest change for me learning Powershell is that contains is only used to find a specific element in a collection. It does not work like String.Contains methods in .net languages which searches for a substring.

    1. Sonny Puijk

      Well, comparison operators behave differently depending on what kind of variable to feed it.

      When you feed an array to .contains it will search for the exact entry. If you feed a string to .contains it will actually search for a substring.

Leave a Reply