Power Shell script beginner in SharePoint 2010

PowerShell in Sharepoint 2010 – Basics Part 1

2 votes, 5.00 avg. rating (97% score)

Introduction

In PowerShell basic series part 1, we can learn how to work with variablesarrays and hashtables. The basic series is very important to understand the complex codes of powershell. This series will be worth reference guide as most of powershell developers are not aware of the syntax and usage.

Usage Variables

Following is the simplest way of creating variables and assign value
1
2
$firstName = "Adi"
$empId = 999
Other commands to create, retrieve and set variables
New-Variable cmdlet is used to create variables with additional features like description and options like setting as readonly variable
Following code creates new readonly variable ‘SiteName’ and assigns value to it. It also provides description what the variable is
1
New-Variable -Name SiteName -Value "PowershellTraining" -Description "Stores name of the site" -Option ReadOnly
Get-Variable cmdlet is used get the value of variable
1
Get-Variable -Name SiteName
Few other variable related cmdlets are Set-Variable, Clear-Variable, Remove-Variable 
In general, simplest way is used rather than cmdlets to create and assign values to the variables

Data Types

When we use assign operator “=”, automatically the suitable datatype will be assigned to the variable.
In the following example we have set values and the variables will become automatically to string and integer variables
1
2
$firstName = "Adi"
$empId = 999
Use GetType() method with FullName property to get the datatype of the variable
1
2
$firstName.GetType().FullName
$empId.GetType().FullName
output will be
System.String
System.Int32
We can also create specific data type variable like the following code
1
[int32]$empId = 32
This will create integer variable directly, rather than powershell to decide the conversion.
If we try to assign string value for the above variable, it will give runtime error.
Other common data types used in powershell are

DataType

Usage

[string][string]$companyName = “AdiCodes”
[int][int32]$empId = 999
[int64][int64]$totalVisits = 9900000
[char][char]$Leaves = 20
[string][bool]$IsVisible = $true
[byte][byte]$byVal = 555
[decimal][decimal]$myDecimal = 11.22
[float][float]$myFloat = 11.22

Automatic variables or Fixed variables

Powershell has predifined fixed variables which can be handy while development.Some of the common fixed variables are

Fixed variable

Description

$$The last token in the last line received by the session
$?True or false, depending on the last performed operation
$^The first token from the last line received by the session
$_The current object in the pipeline object
$argsAn array of values passed to a function or a script
$ErrorAn array of error objects representing the most recent error that occurred
$falseBoolean false
$trueBoolean true
$foreachThe enumerator of a foreach loop
$HomeUser’s home directory
$NULLAn empty value
$PIDThe process identifier of the process that is hosting the current Windows PowerShell session
$ProfileThe full path to the Windows PowerShell profile for the current user
$PsHomeThe full path of the installation directory of Windows PowerShell
$PsScriptRootThe directory from which the script module is being executed
$PsUICultureThe name of the user interface culture that is currently in use in the operating system
$PwdThe current directory

Arrays

An array in PowerShell can contain objects of all types supported by .NET.
Creating an array
1
$EmIds = 11,22,33
or
1
$EmIds = @(11,22,33)
Access or assign array element
1
2
var $firstId = $EmIds[0]
$EmIds[1] = 222
Add an array element
1
$EmIds += "444"
Get array Count
1
$totalEmps = $EmIds.Count
Count is nothing but an alias in System.Array Length property

Hash Tables

HashTables use key value pairs to access values
Creating hashtable
1
$empInfo = @{"FirstName"="Adi";"Company"="AdiCodes"}
Access elements
1
$empInfo["FirstName"]

Conclusion

I hope you got some basic information on variable, arrays and hashtables. We will see in further series what are the various operators and looping in powershell

Comments

Popular posts from this blog

Sending email using Powershell script

Convert List Collection to DataTable using C#

Difference Between Site Template and Site Definition