Quantcast
Viewing all articles
Browse latest Browse all 10

PowerShell in Sharepoint 2010 – Basics Part 2

0 votes, 0.00 avg. rating (0% score)

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”
Output: 30items

- Subtracts values Ex: 10 – 3
Output: 7
* Multiplies two values

(Also multiplys string value with numerics)

Ex: 10 * 3
Output: 30

Ex: “Hi” * 3
Output: HiHiHi

/ 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 += ” ”
$myVariable += “Sharepoint2010″

$myVariable output:

Microsoft Sharepoint2010

−= Decreases the value of a variable by the specified value  Ex:

$myVariable = 10
$myVariable -= 3

$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
$myVariable *= 3
$myVariable output: 30

/= Divides the value of a variable by the specified value  Ex:

$myVariable = 10
$myVariable /= 3

$myVariable output: 3

%= Divides the value of a variable by the specified value and assigns the remainder
to the variable
 Ex:

$myVariable = 10
$myVariable %= 3

$myVariable output: 1

++ Increases the value by one  Ex:

$myVariable = 10
$myVariable ++

$myVariable output: 11

Decreases the value by one  Ex:

$myVariable = 10
$myVariable –
$myVariable output: 9

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:
if(“variable1Value” -eq “variable1Value”)
{
write-host “equal”
}

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:
if(“variable1Value” -ne “variable2Value”)
{
write-host “notequal”
}

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:
if(100 -gt 50)
{
write-host “value is greater”
}

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:
if(50 -lt 100)
{
write-host “value is less”
}Output: value is less

-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:
if(100 -le 100)
{
write-host “either less than or equal”
}

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:
if(100 -ge 100)
{
write-host “either greater than or equal”
}

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
Ex:
“This is test” -like “This is test”
Output : TrueEx:
“This is test” -like “This is*”
Output : True

-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:
“This is test” -notlike “This is test”
Output : FalseEx:
“This is test” -notlike “Is it test*”
Output : True

-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”
Output : This is done

We use also use regular expression.
Using ^ character to match the beginning of the original string.

Ex:
“This is test, this is good” -replace “^This” “That”
Output : That is test, this is good

Conclusion

Hope we got some good understanding on using operators.
We will further check some more basics of powershell to implement in sharepoint 2010.


Viewing all articles
Browse latest Browse all 10

Trending Articles