True and False in scripts is going to be your best friend. It doesn't matter if you are using Windows or Linux, Bash or PowerShell, or even a programming language such as Java. But what is the point of teaching you Boolean if you don't have examples of how they work? As ScriptFreaks, we don't write as programmers but as Admins.
Powershell Boolean, which is based on True or False, is great when being used with If Statements. You can think of Boolean as the original Greater than or Equal to symbols from math.
Example: if you want to test that 2 is greater than or equal to 2.
2 -ge 2
The output of this will result in either True or False. Since 2 is greater than or equal to 2, our result is True. But let's see what happens when we change our first value to 3.
3 -ge 2
The output is True again. Can you guess what happens if we flip these numbers? Is 2 greater than or equal to 3?
2 -ge 3
The output here comes back as False, which is what we expected. But what happens if we change -ge to -le? Le stands for less than or equal to.
2 -le 3
The output this time comes back as True. So unlike math class, where it was easy to mix up the > and < symbols, it's easy to remember -ge and -le. But what if you want to test if two values are equal?
2 -eq 2
The output here will display as True. This is because 2 is equal to 2. If either number did not equal the other, the output here would be false. Such as this example.
2 -eq 3
The output now shows as False. So what are all the symbols you can use in PowerShell for testing?
Our next lesson will apply variables and examples into our Boolean lesson, allowing us to improve our understanding by providing real-world examples. Leave any comments below to let us know if we need to simplify our lessons. We want the world to understand scripting and automation.
Let's improve together.
Commentaires