PowerShell in Sharepoint 2010 – Basics Part 1

PowerShell in Sharepoint 2010 – Basics Part 1
3 votes, 5.00 avg. rating (97% score)

Introduction

In PowerShell basic series part 1, we can learn how to work with variables, arrays 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

$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

New-Variable -Name SiteName -Value "PowershellTraining" -Description "Stores name of the site" -Option ReadOnly

Get-Variable cmdlet is used get the value of variable

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

$firstName = "Adi"
$empId = 999

Use GetType() method with FullName property to get the datatype of the variable

$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

[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
$args An array of values passed to a function or a script
$Error An array of error objects representing the most recent error that occurred
$false Boolean false
$true Boolean true
$foreach The enumerator of a foreach loop
$Home User’s home directory
$NULL An empty value
$PID The process identifier of the process that is hosting the current Windows PowerShell session
$Profile The full path to the Windows PowerShell profile for the current user
$PsHome The full path of the installation directory of Windows PowerShell
$PsScriptRoot The directory from which the script module is being executed
$PsUICulture The name of the user interface culture that is currently in use in the operating system
$Pwd The current directory

Arrays

An array in PowerShell can contain objects of all types supported by .NET.

Creating an array

$EmIds = 11,22,33

or

$EmIds = @(11,22,33)

Access or assign array element

var $firstId = $EmIds[0]
$EmIds[1] = 222

Add an array element

$EmIds += "444"

Get array Count

$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

$empInfo = @{"FirstName"="Adi";"Company"="AdiCodes"}

Access elements

$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

AdiCodesCoverage
Powershell in SharePoint 2010
SharePoint Server 2010 Getting started with windows PowerShell Part 1
PowerShell Basics SharePoint
PowerShell using Arrays
PowerShell using Hash Tables
PowerShell DataTypes in SharePoint

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

Leave a Reply

What is 3 + 12 ?
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) :-)