If you’ve been working with SharePoint for a while and you’ve been happily coding and clicking and STSADM-ing away you need to get skilled up in writing PowerShell scripts as well now.
In my last post I described my first useful PowerShell script. While writing that I bumped in to a couple of things that took me a while to figure out, so I decided to list them in this post to try and save others some time.
The first one is loading another PowerShell file in the current script. This can be useful if you have to load an existing file into your script, like SharePoint.ps1, but it can also be useful if you want to save parts of your script to separate files so you can reuse the different pieces more easily.
Loading a file into a PowerShell script can be done by typing .\ followed by the filename. If the file you want to load is stored in a different directory than the file your currently working on you need to tell PowerShell what directory to look in by using the cd command that should be familiar from the old school command prompt.
cd 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG\POWERSHELL\Registration'
.\SharePoint.ps1
Another nice little trick is creating a variable of the type Array. The first thing you probably noticed is that PowerShell variables aren’t declared. They are just created on first assignment. Something else that stands out is that all variables start with a $. PowerShell variables are polymorphic by default. This means that you can store any type of object in them.In my script I assign strings to the variable, separated by commas. This will get us and array of strings. If I would have assigned numbers separated by commas it would have been an array of integers.
#An array of strings
$users = "john", "paul", "mirjam"
#An array of integers
$users = 5, 6, 7
In the above example you can see that you can write comments by putting a # in front of the text.
The next tip was also explained in my last post. If you put a string in PowerShell in single quotes it is seen as a “Literal string”. This means that the string will be represented exactly as it is displayed within the quotes. A string in double quotes is an “Expandable string”. So a string in double quotes can contain variables and special characters such as newlines and they will be expanded when you run the script.
Suser = "Mirjam"
#String in double quotes
Write-Output "Site created for $user"
#String in single quotes
Write-Output 'Site created for $user'
The output for the above script will be:
Site created for Mirjam
Site created for $user
This doesn’t only work when writing stuff to the screen, it also works in expressions.
The next question of course is how do you know what PowerShell commands are available to you if you want to manage SharePoint using PowerShell.
A very good place to start is here http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=045f7af5-b226-4a05-8ace-4e17cfdef856&utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+MicrosoftDownloadCenter+%28Microsoft+Download+Center%29. This page offers links to downloads of .chm files that describe the PowerShell cmdlets for SharePoint.
You can also get some help from the PowerShell console itself. For instance if you know that the command for creating a new site is New-SPSIte, but you don’t know what parameters you can use on that particular object, you can call the PowerShell help. It doesn’t matter whether you use capitals or not, PowerShell cmdlets are not case-sensitive.
get-help New-SPSite
This line of script will return the following information:
NAME
New-SPSite
SYNOPSIS
Creates a new site collection at the specified URL.
SYNTAX
New-SPSite -Url <String> -OwnerAlias <String> [-AssignmentCollection <SPAssignmentCollection>] [-Confirm [<SwitchParameter>]] [-Con
tentDatabase <SPContentDatabasePipeBind>] [-Description <String>] [-HostHeaderWebApplication <SPWebApplicationPipeBind>] [-Language
<UInt32>] [-Name <String>] [-OwnerEmail <String>] [-QuotaTemplate <SPQuotaTemplatePipeBind>] [-SecondaryEmail <String>] [-Secondar
yOwnerAlias <String>] [-SiteSubscription <SPSiteSubscriptionPipeBind>] [-Template <SPWebTemplatePipeBind>] [-WhatIf [<SwitchParamet
er>]] [<CommonParameters>]
DESCRIPTION
The New-SPSite cmdlet creates a new site collection with the URL and owner specified by the Url and OwnerAlias parameters.
RELATED LINKS
REMARKS
To see the examples, type: "get-help New-SPSite -examples".
For more information, type: "get-help New-SPSite -detailed".
For technical information, type: "get-help New-SPSite -full".
If you want to see some examples or you want more information you can simply use the get-help cmdlet with the –examples, –detailed, or –full parameter.