TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

PowerShell Comparison Operators

PowerShell Comparison Operators with What is PowerShell, History of PowerShell, Features of PowerShell, PowerShell vs CMD, PowerShell Scripting, PowerShell Versions, PowerShell Commands, PowerShell Looping, PowerShell Conditions, PowerShell Array, PowerShell Brackets etc.

<< Back to POWERSHELL

Comparison Operators

The comparison operators are used in PowerShell to compare the values. By default, all comparison operators are case-sensitive. These operators help us to find, test, compare, modify, and replace the data and information.

PowerShell supports the following comparison operators:

PowerShell Comparison Operators
  1. Equality Operators
  2. Matching Operators
  3. Containment Operators
  4. Replacement Operators
  5. Type Operators

Equality Operators

The equality operators are those operators, which check the equality of two values and also check that one value is greater or less than other value.

The following are the list of equality comparison operators:

  1. -eq (Equal)
  2. -ne (Not Equal)
  3. -gt (Greater than)
  4. -ge (Greater than or Equal to)
  5. -lt (Less than)
  6. -le (Less than or Equal to)

-eq (Equal)

If the values are equal, this operator returns the Boolean value TRUE, otherwise False.

Example: The following example describes how to use the -eq (equal) operator:

$a=10
$b=10
($a -eq $b)

The last command in this example displays the Boolean Value TRUE because both the value of the variables are the same.

-ne (Equal)

If the values are not equal, this operator returns the Boolean value TRUE, otherwise False.

Example: The following example describes how to use the -eq (equal) operator:

$a=10
$b=10
($a -ne $b)

The last command in this example displays the Boolean Value FALSE because both the value of the variables are same.

-gt (Greater than)

If the value of a variable on the left side of the operator is greater than the value of a variable on the right side of the operator, this operator returns the Boolean value TRUE, otherwise returns False.

Example: The following example describes how to use the -gt (greater than) operator:

$a=20
$b=10
($a -gt $b)

The last command in this example displays the Boolean Value TRUE because the value of the variable $a is greater than $b.

-ge (Greater than or Equal)

If the value of a variable on the left side of the operator is greater than or equal to the value of a variable on the right side of the operator, this operator returns the Boolean value TRUE, otherwise returns False.

Example: The following example describes how to use the -ge (greater than or Equal) operator:

$a=10
$b=10
($a -ge $b)

The last command in this example displays the Boolean Value TRUE because the value of both the variables $a, and $b are the same.

-lt (less than)

If the value of a variable on the left side of the operator is less than the value of a variable on the right side of the operator, this operator returns the Boolean value TRUE, otherwise returns False.

Example: The following example describes how to use the -lt (less than) operator:

$a=10
$b=20
($a -lt $b)

The last command in this example displays the Boolean Value TRUE because the value of the variable $a is less than $b.

-le (less than or Equal)

If the value of a variable on the left side of the operator is less than or equal to the value of a variable on the right side of the operator, this operator returns the Boolean value TRUE, otherwise returns False.

Example: The following example describes how to use the -le (less than or Equal) operator:

$a=10
$b=10
($a -le $b)

The last command in this example displays the Boolean Value TRUE because the value of both the variables $a, and $b are the same.

Matching Operators

The matching operators are those operators, which compare the strings using regular expression or wildcard characters to find a match.

The following are the list of matching operators:

  1. -like
  2. -notlike
  3. -match
  4. -notmatch

-like

The -like operator returns the Boolean value TRUE if the strings are matched using the wildcard characters.

Examples: The following examples describe how to use the -like operator:

Example1:

$a="PowerShell"
$b="PowerShell"
$a -like $b

The last command, in example1, returns the TRUE value because both the strings are same.

Example2:

$a="PowerShell"
$b="*Shell"
$a -like $b

The last command in this example returns the TRUE value because the "Shell" string is present in the variable $a.

-notlike

The -notlike operator returns the Boolean value TRUE if the strings do not match using the wildcard characters.

Examples: The following examples describe how to use the -notlike operator:

Example1:

$a="PowerShell"
$b="windows"
$a -notlike $b

The last command in the above example returns the TRUE value because the strings do not match.

Example2:

$a="PowerShell"
$b="*Shell"
$a -notlike $b

The last command in this example returns the False value because the "Shell" string is present in the variable $a.

-match

The -match operator returns the Boolean value TRUE if the strings are matched using the wildcard characters. If an input is a list, the -match operator returns the matching members of the list.

Example:

$a="January", "February", "March", "April" 
$b="Feb"
$a -match $b

The last command in this example returns the string "February".

-notmatch

The -notmatch operator returns the Boolean value True when the strings do not match using the wildcard characters.

Examples: The following examples describe how to use the -notmatch operator:

Example1:

$a="PowerShell"
$b="ell"
$a -notmatch $b

The last command in this example returns the FALSE value because the string "ell" is present in the string of variable $a.

Example2:

$a="January", "February", "March", "April" 
$b="Feb"
$a -notmatch $b

The last command in this example returns the following strings:

January
March
April

Containment Operators

The containment operators are similar to the equality operators. These operators always return a Boolean value True, when a value on the right side of the operator exists in the set of values on the left side of the operator, otherwise returns False.

The following are the list of Containment operators:

  1. -contains
  2. -notcontains
  3. -in
  4. -notin

-Contains

This operator returns the Boolean value TRUE when a value on the right side of the operator exists in the set of values on the left side of the operator.

Example:

$a="January", "February", "March", "April" 
$b="March"
$a -contains $b

The last command in this example returns True.

-notContains

This operator returns the Boolean value TRUE when a value on the right side of the operator does not exist in the set of values on the left side of the operator.

Example:

$a="January", "February", "March", "April" 
$b="March"
$a -notcontains $b

The last command in this example returns False.

-in

This operator returns the Boolean value TRUE when a value on the left side of the operator exists in the set of values on the right side of the operator.

Example:

$b="March"
$b="February", "March", "April" 
$a -in $b

The last command in this example returns True.

-notin

This operator returns the Boolean value TRUE when a value on the left side of the operator does not exist in the set of values on the right side of the operator.

Example:

$b="March"
$b="February", "March", "April" 
$a -notin $b

The last command in this example returns False.

Replacement Operator

The replacement operator (-replace) is an operator, which replaces all or some part of value by the specified value using a regular expression.

The following statement is the syntax for the -replace operator:

<input> <operator> <original> , <substitute> 

This syntax uses two arguments: Original and substitute. These are separated by the comma.

Example:

$a="Windows and Linux Operating system"
$a -replace "Linux" , "MacOS"

The second command in this example displays the following output:

Windows and MacOS operating system

Type Comparison Operators

The type comparison operators are those operators, which returns a Boolean value TRUE when the value on the left side of the operator is a specified as a Microsoft .NET type, otherwise returns False.

PowerShell supports the following two type comparison operators:

  1. -is
  2. -isnot

-is

This operator returns the True when the value on the left side of the operator is a specified as a Microsoft .NET type.

Example:

$a=1
$a -is [int]

The second command in the example displays a Boolean value True because the value of the variable $a is an integer.

-isnot

The -isnot operator returns the Boolean value True when the value on the left side of the operator is not specified as a Microsoft .NET type.

Example:

$a="1"
$a -is [int]

The second command in the example displays a Boolean value True because the value of the variable $a is a string.


Next TopicLogical Operators




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf