SharePoint 2010: Common PowerShell Snippets
Introduction
This wiki has been created to consolidate the SharePoint PS snippets we use on our day to day SharePoint activities.
General
Adding the SharePoint SNAPIN
# Add SharePoint Snapin to PowerShell
if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null)
{
Add-PSSnapin Microsoft.SharePoint.PowerShell
}
Solution Deployment
Add solution to farm
#Replace the wsp path with the actual path of your wsp
Add-SPSolution -LiteralPath C:\Rajesh\Sample.wsp
Deploy Solution
#Replace the wsp name and WebApplication url
Install-SPSolution -Identity Sample.wsp -WebApplication "http://contoso.com" –GACDeployment
There are multiple parameters to the install-spsolution cmdlet, refer to http://technet.microsoft.com/en-us/library/ff607534.aspx
Remove Solution
#Replace the wsp name and WebApplication url
Uninstall-SPSolution -Identity Sample.wsp -WebApplication
Enable Feature
#Replace the feature identity and WebApplication url
Enable-SPFeature –identity "Rajesh.CustomFeature1" -URL "http://contoso.com"
Disable Feature
#Replace the feature identity and WebApplication url
Disable-SPFeature –identity "Rajesh.CustomFeature1" -URL "http://contoso.com"
Uninstall Feature
#Removes the feature definition from the farm.
#Replace the feature identity
Uninstall-SPFeature –identity "Rajesh.CustomFeature1”
Branding
Changing the master page on all sites at site collection
#Replace the site-url with the actual site url
$site = Get-SPSite "http://contoso.com"
foreach ($web in $site.AllWebs) {
#change the master url accordingly
$web.CustomMasterUrl = "/_catalogs/masterpage/V4.master";
$web.Update();
$web.Dispose();
}
foreach ($web in $site.AllWebs) {
#change the master url accordingly
$web.MasterUrl = "/_catalogs/masterpage/v4.master";
$web.Update();
$web.Dispose();
}
$site.Dispose();
Set Alternate CSS
#Replace the site-url with the actual site url
$web = Get-SPWeb "http://contoso.com"
#Replace the css url accordingly
$web.AlternateCssUrl = "/Style Library/MyStyles/main.css";
$web.AllProperties["__InheritsAlternateCssUrl"] = $True;
$web.Update();
$web.Dispose();
Set regional setting/locale
#Replace the site-url with the actual site url
$site = Get-SPSite "http://contoso.com"
foreach ($web in $site.AllWebs) {
#Change the Web locale as required, below is for US Eng
$web.Locale = 1033;
$web.Update();
$web.Dispose()
}
$site.Dispose()
Document Library
Delete document by name from Document Library
$site = new-object Microsoft.SharePoint.SPSite("site-url")
$web = $site.openweb()
$list=$web.Lists["Document-Library-Name "]
$listItems = $list.Items
$listItemsTotal = $listItems.Count
Write-Host $listItemsTotal
for ($x=$listItemsTotal-1;$x -ge 0; $x--)
{
if($listItems[$x].name.Contains("file")) # file refers to the name of the document
{
Write-Host("DELETED: " + $listItems[$x].name)
$listItems[$x].Delete()
}
}
Site Management
Delete all sites in the given site
# completely deletes the specified Web (including all sub sites)
function RemoveSPWebRecursively( [Microsoft.SharePoint.SPWeb] $web)
{
Write-Debug "Removing site ($($web.Url))..."
$subwebs = $web.GetSubwebsForCurrentUser()
foreach($subweb in $subwebs)
{
RemoveSPWebRecursively($subweb)
$subweb.Dispose()
}
$DebugPreference = "SilentlyContinue"
Remove-SPWeb $web -Confirm:$false
$DebugPreference = "Continue"
}
$DebugPreference = "SilentlyContinue"
#Replace the site-url with the actual site url
$web = Get-SPWeb "http://contoso.com/subsite1"
$DebugPreference = "Continue"
If ($web -ne $null)
{
RemoveSPWebRecursively $web
$web.Dispose()
}
Power-shell SharePoint 2010 Developer Dashboard activate the developer dashboard
$service = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
$addsetting =$service.DeveloperDashboardSettings
$addsetting .DisplayLevel = [Microsoft.SharePoint.Administration.SPDeveloperDashboardLevel]::OnDemand
$addsetting .Update()
Other languages
This article is also available in the following languages: