PowerShell in Sharepoint 2010 – Basics Part 3

PowerShell in Sharepoint 2010 – Basics Part 3
0 votes, 0.00 avg. rating (0% score)

Introduction

We learned how to use variables, arrays and hashtables in Powershell basics part 1. We learned how to use arithmetic, assignment and comparison operators in Powershell basics part 2. Now we will check how we can use logical operators and redirection operators



Logical Operators

Following is the list of logical operators and their usage

Operator

Description

Usage

-and Returns true when both left value and right value of operand are true The –and operator is similar to && operator in c#.

Ex:
if((10 -eq 10) -and (20 -eq 20))
{
write-host “both are equal”
}
Output : both are equal

-or Returns true when either of the left value or right value of operand is true The –or operator is similar to || operator in c#.

Ex:
if((10 -eq 10) -or (20 -eq 10))
{
write-host “both or at leaset one is equal”
}
Output : both or at leaset one is equal

-xor Returns true when only one is true in either of the right and left values of operand The –xor operator is similar to ^ operator in c#.

Ex:
if((10 -eq 10) -xor (20 -eq 10))
{
write-host “only one is true”
}
Output : only one is true

! or -not This negates the resultant boolean value, true to false or false to true The ! operator is similar to ! operator in c#.

Ex:
if(!(10 -eq 20))
{
write-host “values are not equal”
}
Output : values are not equal

Redirection Operators

Following is the list of redirection operators and their usage

Operator

Description

Usage

> Sends output to a file The following example will create new commandlog.txt file with the output of the cmdlet

Ex:
Get-Command > commandlog.txt

>> Appends output to a file The following example will not create new commandlog.txt file everytime but appends the output of the cmdlet to the same txt file

Ex:
Get-Command >> commandlog.txt

2> Sends errors to file The following example will create new commandlog.txt file with the errors that occured by the cmdlet. No errors will be displayed on the powershell console application

Ex:
Get-Command 2> commandlog.txt

2>> Appends errors to file The following example will create new commandlog.txt file with the errors that occured by the cmdlet but only appends to the existing file.
No errors will be displayed on the powershell console application

Ex:
Get-Command 2>> commandlog.txt

Conclusion

Hope you got some good understanding on the various operators in powershell and how they are used.
Any SharePoint / .net developer once understands the syntax and basic usage they can pile many advanced scripts 😉
We will further see how we can use flow controls (arrays, for loop, foreach…) and how to create functions.

April 20, 2012 · Adi · No Comments
Tags: , , , , ,  · Posted in: Operators, Packaging and Deployment, Powershell, Sharepoint 2010

Leave a Reply

What is 7 + 11 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)