PowerShell if not logic is pretty easy. Below are some quick examples and explanations for you. The structure of if block is given below.
What that means is, when something is correct, do this. And the something is given for the condition and the this is for the command. It is easy to understand once you do a couple of examples from this post.
Here the -eq is equal to operator. That means, if 3 is equal to 3, do what is inside the curly braces. And inside the curly braces, we have ‘Yes’.
We get the output as shown in the image.
Taking it one step further we will check if the current OS is Windows 10 or not. This time we will use the -like for matching if the os name contains the term windows 10. If it contains windows 10 we will print yes and if it does not contain windows 10 we will print no.
We get the below output.
Now we will go the real topic of this post and that is if not. We will check if the os is not windows 10 and it should return no because the OS is indeed windows 10. We can check if something is not what it is by just adding the -not parameter in front of the condition.
Here we check if the OS is not windows 10. From the previous example, we know that the OS is Windows 10 so it is not no windows 10. We get no in the output as shown in the image.
-ne is the short form of not equal. We can use the script as follows.
It produces the output as follows.
In this post, we learned about the if-not. We can use the if condition block in a generic way with the help of -eq operator. The -eq operator will help us compare if two entities are equal. And the -not parameter in-front of the condition will check if the two entities are not equal or what. We can use the -ne instead of -eq to directly check for the not equal condition.