Chapter 5 – Using PowerShell with Sharepoint 2010

Wrox Real World Sharepoint 2010 (Page 129)

The alias of Get-Help is help.

Right click the Sharepoint Management Shell
command, this is the link:
C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe -NoExit
” & ‘ C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG\POWERSHELL\Registration\\sharepoint.ps1
‘ “

$ver = $host | select version

if ($ver.Version.Major -gt 1) {$Host.Runspace.ThreadOptions = “ReuseThread”}

Add-PsSnapin Microsoft.SharePoint.PowerShell

Set-location $home

Script editor: Windows Powershell Integrated Scripting Environment (ISE)

You need to manually run this command in ISE:
Add-PsSnapin Microsoft.SharePoint.PowerShell

Get-Member takes in any object and outputs info about the object’s public members.
Get-Member –InputObject $farm

In Powershell, an object’s type is represented by wrapping it in brackets (“[…..]”)

To instantiate a new object, you use New-Object

When declare a variable, you can do this:
$farm = Get-SPFarm or
[Microsoft.Sharepoint.Administration.SPFarm]$farm = Get-SPFarm (explicit declaration)

Read XML contents into a variable
[xml]$settings = Get-Content .\FarmConfiguration.xml

Initiate a hash table: $user = @{}

Wrapping in parentheses enable Powershell to evaluate, $($

Create an array:
$users=”Tessa”, “Pamela”

Object Pipe Line – pass an object from one command to another.

returns all commands with “Format”
Get-Command –verb Format

Filtering and Iterating —
Get-SPSite –Limit All | where {$_.Owner –like “wrox\spadmin”}
Get-SPSite –Limit All –Filter {$_.Owner –eq “wrox\spadmin”}

1..10 | ForEach-Object {

if ($_ -eq 5) {continue}

Write-Host $_


Functions and Scripts —

example: register an user account as a managed account.
function New-ManagedAccount {

$cred = Get-Credential “wrox\spfarm”

return New-SPManagedAccount -Credential $cred


save it as Function1.ps1 and run it and it will be loaded for later use. Or, make the account name a parameter:
function New-ManagedAccount([string]$accountName) {

$cred = Get-Credential $accountName

return New-SPManagedAccount -Credential $cred


function New-ManagedAccount([string]$accountName) {

$account = Get-SPManagedAccount $accountName -ErrorAction SilentlyContinue

if ($account -eq $null) {

$cred = Get-Credential $accountName

$account = New-SPManagedAccount -Credential $cred


return $account


Script file:

[string]$settingsFile = “Configurations.xml”


[xml]$settings = Get-Content $settingsFile

$settings.Farm.Accounts.Account | ForEach-Object {

New-ManagedAccount $_.Name


To run this script (with a parameter) do either one:
(1) CreateManagedAccounts.ps1 “configurations.xml”
(2) CreateManagedAccounts.ps1 –settings “configurations.xml”

gcm: Get-Command
gm: Get-Member

PipeBind Objects:
Objects that can be represented with different forms, such as SPWeb, SPSite: You can use URL, GUID, or site variable:
$site = Get-SPSite http://portal

$webs = $site | Get-SPWeb

$webs = $site.ID | Get-SPWeb

$webs = “http://portal” | Get-SPWeb

Commonly used cmdlets: FARM


Creates a new config database, and therefore, a new farm. Run only once each farm.


Connects a server to a config database – thus adding the server to the farm. Once per server in the farm.


Set required file, folder and registry access control lists (ACLS) for local server (must run once each server in the farm)


Install service sin the farm. Once per farm.


Install all the features available to the farm. Run once per farm using –AllExistingFeatures param.


Provision a Central Administration site on the local server. Typically run once per farm, but can be run on additional server as needed.


Install Help files


Installs any application content for the Central Admin. Run only once per farm on the same server as the first Central Admin site.

Commonly used cmdlets: SITE STRUCTURE


Create new managed account


Creating application pools for service applications.


Create a new web application


Creates a new content database for a specific web application


Create a new managed path for web application


Create new site collection


Create a new web


Set the actions that users can perform using Sharepoint Designer

Handling Disposable Objects —
A particular parameter is available to every cmdlets:
-AssignmentCollection <SPAssignmentCollection>

Purpose: store objects that implement IDisposable interface – such as SPSite, SPWeb and SPSiteAdministration.

<Global Assignment> Example:
Start-SPAssignment –Global
Stop-SPAssignment –Global

Creating Custom Cmdlets —

Post a comment or leave a trackback: Trackback URL.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: