So let's begin.
First up, let's take a look at comparison operator.
With a comparison operator, we're going to compare two values.
The operation will return a result of either True or False.
Now when we're comparing things,
if we're comparing numbers obviously that's pretty straightforward,
if we're comparing strings, PowerShell by default is not case sensitive,
although there are ways that you can make it case
sensitive if you need to do that.
And here's how these operators work.
All of the comparison operators,
and really all operators in general in PowerShell,
always start with a dash and then whatever the operator name is.
For example, in the comparison operator world there is greater than,
and in PowerShell we use ‑gt. So is 5 ‑gt 2,
and PowerShell will give you a result of True.
Greater than or equal to, is 2 ‑ge 2? Well obviously it is,
so the result is True.
Less than, 5, is it ‑lt 2?
And of course that's not the case, so we get False.
Less than or equal to,
so I hope you're beginning to see a pattern here on how these operators work.
Nothing really cryptic or complicated.
Let's move on.
I'm going to set $i, remember that's a variable we looked at in the last module,
and I'm going to give that a value of 7.
The equal sign is the assignment operator.
If you want to compare equality, that is the ‑eq operator.
So is $i = 7?
And yes, we get a result of True.
If $i is not equal to 7, then we get a result of False.
Now string comparisons, as I mentioned, are not case sensitive,
so lowercase jeff is in fact equal to capital case JEFF,
and PowerShell will give me a result of True.
But if I want to make it case sensitive,
there is a way with some of the equality operators to
put a c in front of the operator, and that tells PowerShell,
hey, make this case sensitive.
So now lowercase jeff ‑ceq JEFF, that comparison is not valid,
and so I get a result of False.
The other way that we can do comparisons is with a wildcard operator like ‑like,
and here we use the wildcard character, the asterisk.
So is the string foo ‑like f, and then whatever follows after the letter f,
and of course that is True.
To do the opposite comparison, we can use ‑notlike.
So here we have an example where bar is ‑notlike B*,
and even though I'm using a capital letter,
again, comparisons are not case sensitive,
and so I get a result of False.
Like I showed you in the last slide,
we can make it case sensitive by putting that c in front of it,
so now I do bar ‑clike B*, and again that's going to be False.
There is also a way to match regular expressions in
PowerShell with the ‑match operator.
So here I can take a string on the left side,
abc‑1234, and say does it match a regular expression operator of \d+.
So I need one or more digit characters, is it in that string,
and because it is, I get a result of True.
Now, I'll admit this is an advanced topic,
we will not be getting into regular expressions,
but if you already understand how regular expressions
work and how to build a pattern, you can use the ‑match operator.
There is a help topic you can look at if you want a refresher to get started,
Help About_Regular_Expressions,
and there are some other courses in the Pluralsight
library on PowerShell and regular expressions if you want
to dive into this topic further.
You also have a way to test a non‑match with the ‑notmatch operator,
so 1234 does it ‑notmatch.
Again, on the right side is the regular expression pattern,
one or more digits,
and that gives the result of False because there are digits in that string.
Here's an example of how you might use a comparison operator in PowerShell.
So I'm going to do Get‑Process,
and then get all the processes where the company name matches Logitech,
and then I'm selecting some properties.
So you can see there are a couple of processes,
and in some processes it's using Logitech,
and sometimes it's using Logitech, Inc.
So the regular expression operator ‑match allows me to get
all the processes from a related company.
And I'm kind of doing the same thing with the second example,
where I'm getting all the processes where the name of the company
does not match something that starts with micro,
like Microsoft, and then grouping them on the company,
so you can see all of the other company names associated with
the processes running on this computer.
Again,
this is a nice way of showing you why and how you might want to use these comparison operators.