PowerShell Operators Basically PowerShell comparison operators are declared using the symbol dash (-) followed by a name (eq for equal, gt for greater than, -lt as less than and more.
Comparison operators:
- 4 -eq 4 # Equal to (=)
- 3 -ne 5 # Not equal to (!=)
- 6 -gt 5 # Greater-than (>)
- 15 -ge 15 # Greater-than or equal to (>=)
- 3 -lt 10 # Less-than (<)
- 3 -le 3 # Less-than or equal to (<=)
String comparison operators:
“rajashekar” -like “*shekar” # This comparison operator used for match using the wildcard character (*)
“rajashekar” -notlike “prava*” # This comparison operator used to check the string does not match using the wildcard (*).
“mylove” -match ‘mylove $’ # This comparison operator used to for matching a string using regular expressions.
“hello” -notmatch ‘hi’ # This comparison operator used to check as not match a string using regular expressions.
PowerShell Collection comparison operators:
“raja -contains “raj” # This comparison operator used for Returning a true when the value (right) is present
“pravaf” -notcontains “123” # This comparison operator used for returning a true when the value (right) is not present
“raja” -in “raja”, “raj” # This comparison operator returns true when the value (left) is present
“143143” -notin “234”, “345” # This comparison operator returns true when the value (left) is not present.
PowerShell Arithmetic Operators
- + This operator used for an addition.
- – This operator used for Subtraction.
- # This operator used for value.
- * This operator used for multiplication.
- / This operator used for division.
- % This operator used for modulus.
PowerShell Logical Operators
- -and This operator used logical and.
- . -or This operator used for logical or.
- -xor This operator used for logical exclusive or. 4
- -not This operator used for logical not.
- ! This operator used for logical not.
PowerShell Assignment Operators
- = This assignment operator Sets the value of a variable to the specified value
- += This assignment operator Increases the value of a variable by the specified value
- -= This assignment operator Decreases the value of a variable by the specified value.
- /= This assignment operator Divides the value of a variable by the specified value.
- %= This assignment operator Divides the value of a variable by the specified value and then.
1) PowerShell if statement:
If a conditional logic operator used for running the code block if a specified conditional test is true.
Syntax of if–>
if ( condition ) { commands_to_execute }
[ elseif ( condition2 ) { commands_to_execute } ]
[ else {commands_to_execute} ]
Examples:
If ($scripter -eq "I know powershell") { Write-host " one can write powershell scripts" } # Two use more than one if statement:
Exalmple 2
# Two use more than one if statement: If ($scripter -eq "I know powershell") { Write-host " one can write powershell scripts" } Elseif ($scripter -eq " I don't know powershell") { Write-host "You need to learn powershell" }
# Negation if statement $test = "raja" If($test -ne "prava") { Write-host "I found raja" }
Thank you for reading this article, if you have any questions please let us know.
Thank you for visiting my site, please make sure you have tested this script in our lower environment before you run in production.
Leave a Reply Cancel reply