in

WindowsWindows Loving ItLoving It

PowerShell if not Windows 10 [2020]

PowerShell if not -ne operator

powershell if not

PowerShell if not logic is pretty easy.  Below are some quick examples and explanations for you.  The structure of if block is given below.

if(condition){Block Command}

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.

if(3 -eq 3){‘yes’}

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.

3 equal 3
3 equal 3

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.

if($n -like ‘*windows 10*’){‘yes’}else {‘no’}

We get the below output.

is windows 10?
is windows 10?

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.

if(-not $n -like ‘*windows 10*’){‘yes’}else {‘no’}

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.

not windows 10?
not windows 10?

-ne is the short form of not equal.  We can use the script as follows.

if(3 -ne 4){‘yes’} else {‘no’}

It produces the output as follows.

3 is 4?
3 is 4?

Conclusion:

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.

 

PowerShell if not
  • Article Rating
  • Content Relevance
  • Content Clarity
  • Author Rating
4.6

Leave a Reply

Your email address will not be published. Required fields are marked *

Loading…

0

Comments

0 comments