Documente Academic
Documente Profesional
Documente Cultură
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.
Enable-PSRemoting
To connect to the SharePoint server from your workstation in interactive mode:
Getting Started
To start it locally: On the Start menu, click SharePoint 2010 Management Shell, Or in a regular PowerShell session, execute: Add-PSSnapin Microsoft.SharePoint.Powershell
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).
Implicit Remoting
To execute a PowerShell script on a remote server from local machine without opening an interactive session and typing Invoke-Command 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:
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.
$session = New-PSSession 'MySharePointServer' Credential:'Domain\Username' Invoke-Command $session { Add-PSSnapin Microsoft.SharePoint.Powershell } Import-PSSession -session $session Get-SPSite "http://mycompany/sites/mysite"
Getting Help
Get help for a cmdlet:
Get-Help Get-SPSite
Get a list of all SharePoint cmdlets:
$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 prompted for the credentials to connect to a remote session, if required.
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. To enable PowerShell remoting on the SharePoint server:
Po
cheat sheet version 2.0
rG we
UI
Get-SPWeb $url | Select -Expand Lists | Select -Expand Items | select Name, Url
Show only documents:
Get-SPSite | Remove-SPSite
To remove all site collections without confirmations:
Get-SPWeb $url | Select -Expand Lists | Where {$_.BaseType -eq "DocumentLibrary"} | Select -Expand Items | select Name, Url
Search for item:
New-SPSite $url -OwnerAlias $admin -Template (Get-SPWebTemplate | Where {$_.Title -eq "Team Site" } )
To create new site:
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:
function New-SPFile($WebUrl, $ListName, $DocumentName, $Content) { $stream = new-object System.IO.MemoryStream $writer = new-object System.IO.StreamWriter($stream) $writer.Write($content) $writer.Flush() Get-SPWeb $WebUrl | ForEach {$_.Lists[$ListName]} | ForEach {$_.RootFolder.Files.Add($DocumentName, $stream, $true);$_.Update()} } New-SPFile -WebUrl "http://mycompany/sites/mysite" -ListName "Shared Documents" -DocumentName "MyFirstDocument" -Content "Power Blues"
SharePoint delays execution of some of its tasks using timers. These may fail or be set up to execute too often and overload the server thus making other tasks fail. To get a list of all timer jobs:
Recycle Bin
To find an item by its name in the Recycle Bin for a site:
Get-SPTimerJob
To get a list of job failures grouped by the job name:
Get-SPTimerJob | Select -Expand HistoryEntries | Where {$_.Status -ne "Succeeded"} | group JobDenitionTitle
Po
rG we
UI
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-SPAssignment
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
Po
rG we
UI