Professional Documents
Culture Documents
SharePoint 2010 ships with some 531 PowerShell cmdlets giving administrators ultimate power over their SharePoint server. This quick cheat sheet will help you get started with it.
Getting Started
SharePoint PowerShell snapin (command library) is installed on any server on which you performed a "complete" installation of SharePoint software (SharePoint 2010 front end server or application server). To start it locally: On the Start menu, click SharePoint 2010 Management Shell, Or in a regular PowerShell session, execute: Add-PSS napin Microsoft.SharePoint. Powershell
Permissions
To grant these permissions sufficient to use PowerShell for SharePoint 2010, use Add-SPShellAdmin cmdlet and specify the user and the databases to which the user needs access, e.g.
Getting Help
Get help for a cmdlet:
Get-Help Get-SPSite
Get a list of all SharePoint cmdlets:
Get-Command *Backup*
Finding out which properties and methods an object emitted by a cmdlet has:
Get-SPWeb | Get-Member
Remoting
To invoke cmdlets from a remote machine (for example, your workstation rather than SharePoint server) you can use PowerShell remoting.
Enable -PSRemoting
To connect to the SharePoint server from your workstation in interactive mode:
Implicit Remoting
To execute a PowerShell script on a remote server from local machine without opening an interactive session and typing Invoke-Comma nd each time you can set up implicit remote session, in which case all SharePoint cmdlets will appear to be executing locally while in reality these will be so called "proxy functions " invoking their corresponding originals on the SharePoint server:
$session = New-PSSession 'MySharePointServer' Credential:'Domain\Username' Invoke-Command $session { Add-PSSnapin Microsoft.SharePoint.Powershell } Import-PSSession -session $session Get-SPSite "http://myco mpan y/ sites/mysite "
$session = New-PSSession 'MySharePointServer' Credential:'Domain\Username' Invoke-Command $session { Add-PSSnapin Microsoft.SharePoint.Powershell } Export-PSSession -Session $session -OutputModule "SP2010" - CommandName *-SP*
This will create a module by name SP2010 at the $env:PSModulePath. You can import this module like any other PowerShell module using Import-Module and get access to the remote cmdlets as if they were on the local machine. PowerShell takes care of creating a remote session as required and you will be prompte d for the credentials to connect to a remote session, if required.
New-SPSite $url -OwnerAlias $admin -Template (GetSPWebTemplate | Where {$_.Title -eq "Team Site" } )
To create new site:
Get-SPTimerJob
To get a list of job failures grouped by the job name:
Get-SPWeb $url | Select -Expand Lists | Select -Expand Items | select Name, Url
Show only documents:
Get-SPWeb $url | Select -Expand Lists | Where {$_.BaseType -eq "DocumentLibrary"} | Select -Expand Items | select Name, Url
Search for item:
$writer.Write($content) $writer.Flush() Get-SPWeb $WebUrl | ForEach {$_.Lists[$ListName]} | ForEach {$_.RootFolder.Files.Add($DocumentName, $stream, $true); $_.Updat e()} } New-SPFile -WebUrl "http://mycompany/sites/mysite" -ListName "Shared Documents" -DocumentName "MyFirstDocument" -Content "Power Blues"
Get-SPWeb $url | Select -Expand Lists | Select -Expand Items | Where {$_.Name -like "*.doc"} | select Name, Url
To create a new document in a document library:
Recycle Bin
To find an item by its name in the Recycle Bin for a site:
function New-SPFile($WebUrl, $ListName, $DocumentName, $Content) { $stream = new-object System.IO.MemoryStream $writer = new-object System.IO.StreamWriter($stream)
(Get-SPSite).RecycleBin
SharePoint Backup
To start a full farm backup,
Backup-SPFarm -ShowTree
To perform a site collection backup:
Object disposal
By default, SharePoint PowerShell tends to dispose the objects at the end of the pipeline. This means that variables assigned to pipeline output might lose data once the one-liner is finished. If you need the data to persist in memory and want to make SharePoint PowerShell store results beyond one-liners run:
Start-SPAssignment
To get back to default behavior (dispose once the pipeline is finished):
Stop-SP Assignment
Useful Links
Free PowerShell community, forums, administrative and scripting/debugging tools: http://PowerGUI.org Get latest version of this cheat sheet at: http://powergui.org/entry.jspa?externalID=2812
Gotchas
There are a few SharePoint gotchas to keep in mind:
Contributors
Dmitry Sotnikov http://dmitrysotnikov.wordpress.com Mike Plavsky http://maplpro.blogspot.com
Ravikanth Chaganti http://www.ravichaganti.com/blog Konstantin Vlasenko http://vlasenko.org/ Konstantins PowerSlim project (Fitnesse + PowerShell): http://powerslim.codeplex.com/
cheat sheet version 2.0