Introduction
We learned how to use variable, arrays and hashtables in Powershell basics part 1. We will check how we can use arithmetic operators, assignment operators and comparision operators in powershell
Arithmetic Operators
Following is the list of arithmetic operators and their usage
Operator |
Description |
Usage |
+ | Adds two values | Ex: 10+20 Ouput: 30Ex: “Hi” + “There” Output: HiThere Ex: [string]30 + “items” |
- | Subtracts values | Ex: 10 – 3 Output: 7 |
* | Multiplies two values
(Also multiplys string value with numerics) |
Ex: 10 * 3 Output: 30 Ex: “Hi” * 3 |
/ | Divide one value by another | Ex: 10 / 3 Output: 3 |
% | Returns the remainder from devision | Ex: 10 % 3 Output: 1 |
Assignment Operators
Following is the list of
assignment operators and their usage
Operator |
Description |
Usage |
= | Sets the value of a variable to the specified value | Ex: $myVariable = 1 |
+= | Increases the value of a variable by the specified value or appends to the existing value |
Ex:
$myVariable = “Microsoft” $myVariable output: Microsoft Sharepoint2010 |
−= | Decreases the value of a variable by the specified value | Ex:
$myVariable = 10 $myVariable output:7 |
*= | Multiplies the value of a variable by the specified value or appends the specified value to the existing value |
Ex:
$myVariable = 10 |
/= | Divides the value of a variable by the specified value | Ex:
$myVariable = 10 $myVariable output: 3 |
%= | Divides the value of a variable by the specified value and assigns the remainder to the variable |
Ex:
$myVariable = 10 $myVariable output: 1 |
++ | Increases the value by one | Ex:
$myVariable = 10 $myVariable output: 11 |
– | Decreases the value by one | Ex:
$myVariable = 10 |
Comparision Operators
Following is the list of comparison operators and their usage. This is one of the
frequently used operators with different syntax not similar to c#.
So, most of the developers tend to look for the reference how to use comparison operators
Note that powershell is not case sensitive. Comparision operators will resemble operators in c# but we have
to exclude the case sensitivity.
Operator |
Description |
Usage |
-eq | equal to | -eq operator is same as “==” in c#
If either side of the ‘-eq’ operator values are equal, it returns true Ex: Output: equal |
-ne | Not equal to | -ne operator is same as “!=” in c#.
If either side of the ‘-ne’ operator values are not equal, it returns true Ex: Output: notequal |
-gt | greater than | -ne operator is same as “>” in c#.
If the left value of ‘-gt’ operator is greater than right value,it returns true. Ex: Output: value is greater |
-lt | less than | -ne operator is same as “!=” in c# If the left value of ‘-lt’ operator is less than right value,it returns true. Ex: |
-le | less than or equal to | -le operator is same as “<=” in c# If the left value of ‘-le’ operator is less than or equal to right value,it returns true. Ex: Output: either less than or equal |
-ge | greater than or equal to | -ge operator is same as “>=” in c#.
If the left value of ‘-ge’ operator is greater than or equal to right value,it returns true. Ex: Output: either greater than or equal |
-like | Match with wildcard character (*) | The –like and –notlike operators are similar to the –eq and –ne operators, but instead of matching exact values, they allow wildcards to be used.
For case sensitivity comparison, use ‘-clike’ operator |
-notlike | Does not Match with wildcard character (*) | The –like and –notlike operators are similar to the –eq and –ne operators, but instead of matching exact values, they allow wildcards to be used.
Ex: |
-match | Evaluates a regular expression against the operand on the left; returns True if the match is successful. Operator ‘-cmatch’ is nfor case sensitive |
The –match and –notmatch operators try to match one or more of the set of string values on the left side of the operation using regular expressions. Ex: “This is test” -match “This is test” Output : TrueEx: regular expression that checks if the string starts with “This” “This is test” -match “^This” Output : True |
-notmatch | Evaluates a regular expression against the operand on the left; returns True if the match is not successful |
The –match and –notmatch operators try to match one or more of the set of string values on the left side of the operation using regular expressions. Ex: “This is test” -notmatch “This is test” Output : FalseEx for regular expression that checks if the string starts with “This” “This is test” -notmatch “^This” Output : False |
-replace | Replaces all or part of a value with the specified value using regular expressions | You can use the -replace operator to search for and replace a specific pattern.
Ex: Example that replace the word “This is test” -replace “test” “done” We use also use regular expression. Ex: |
Conclusion
Hope we got some good understanding on using operators.
We will further check some more basics of powershell to implement in sharepoint 2010.