Sunday, January 9, 2011

PowerShell for SharePoint

First things first. Where can we find PowerShell? When running on a SharePoint server, two possibilities exist: either select the SharePoint 2010 Management Shell from the Start menu or open a command prompt and enter the following:


If we’re using the SharePoint management shell, the SharePoint snap-in will already be installed. If we’re using a standard PowerShell console, we can install the snap-in by entering the following command:

Add-PSSnapIn Microsoft.SharePoint.PowerShell

We can check the list of installed snap-ins by using this command:



Connecting to SharePoint Remotely

We can open a PowerShell session on a client machine and then use remoting to connect to a SharePoint server. To enable remoting on the server, enter the following command:

This command will enable the WinRM service and set up the firewall to allow incoming sessions. Now, we can connect from any client machine by entering the following command:
Enter-PSSession "Server Name" -Credential (Get-Credential)

PowerShell Permissions

To use SharePoint cmdlets, a user must be a member of the SharePoint_Shell_Access role for the farm configuration database as well as a member of the WSS_ADMIN_WPG group on the SharePoint front-end server. To grant users the appropriate permissions, use the
following command:

Add-SPShellAdmin -Username domain\username -database (Get-SPContentDatabase-webapplication http://weburl)


Working with Site Collections and Sites

Most of the cmdlets commonly used in the management of site collections or sites end in SPSite or SPWeb. To pick up a reference to a site collection, we can use the following:
$site=Get-SPSite -Identity http://siteurl
Or we can return a list of all site collections by using this:

When it comes to managing site objects (SPWeb), we can pick up a specific web site using this:
Get-SPWeb -Site http://SiteUrl


Get-SPWeb -Site $site


Creating Site Collections and Sites

Create a new site collection using the New-SPSite cmdlet:

New-SPSite -Url http://localhost/Sites/NewSiteCollection - OwnerAlias username

Add new sites using the New-SPWeb cmdlet: 

Deleting Site Collections and Sites

We can delete site collections and sites by using the Remove-SPSite or the Remove-SPWeb cmdlet.


Setting Properties on SharePoint Objects

$web=SP-GetSPWeb -Identity http://myweburl
$web.Title="My New Title"


Working with Lists and Libraries

Enumerate the lists on a site using the following:

Get-SPWeb -Identity http://myweburl | Select -Expand lists| Select Title

Add new lists using the Add method of the Lists property:

Get-SPWeb -Identity http://myweburl | ForEach {$_.Lists.Add("My Task List", "",$_.ListTemplates["Tasks"])}

Working with Content

Retrieve a list of all items in a site using the following:

Get-SPWeb -Identity http://myweburl | Select -Expand Lists | Select -Expand Items | select Name, Url

Apply a filter to show only documents:

Get-SPWeb -Identity http://myweburl | Select -Expand Lists | Where {$_.BaseType -eq "DocumentLibrary"} | Select -Expand Items | select Name, Url

Use of filters to search for a specific item:

Get-SPWeb -Identity http://myweburl | Select -Expand Lists | Select -Expand Items | Where {$_.Name -like "foo*"} | select Name, Url

Creating New Documents

To create a new document in a document library, use the following:

function New-SPFile($WebUrl, $ListName, $DocumentName,$Content)
$stream = new-object System.IO.MemoryStream
$writer = new-object System.IO.StreamWriter($stream)
$list=(Get-SPWeb $WebUrl).Lists.TryGetList($ListName)
$file=$list.RootFolder.Files.Add($DocumentName, $stream,$true)
New-SPFile -WebUrl "http://myweburl" -ListName "Shared Documents" -DocumentName "PowerShellDocument.txt" -Content "Document Content"


Working with Timer Jobs

Get a list of all timer jobs:


Or we can get a list of job failures grouped by the job name:

Get-SPTimerJob | Select -Expand HistoryEntries | Where {$_.Status -ne "Succeeded"} | group JobDefinitionTitle

No comments: