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

1.# Add SharePoint Snapin to PowerShell
2.if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
3.  Add-PSSnapin Microsoft.SharePoint.PowerShell
4.}

Solution Deployment

Add solution to farm

1.#Replace the wsp path with the actual path of your wsp
2.Add-SPSolution -LiteralPath C:\Rajesh\Sample.wsp

Deploy Solution

1.#Replace the wsp name and WebApplication url
2.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

1.#Replace the wsp name and WebApplication url
2.Uninstall-SPSolution -Identity Sample.wsp -WebApplication http://contoso.com –GACDeployment

Enable Feature

1.#Replace the feature identity and WebApplication url
2.Enable-SPFeature –identity "Rajesh.CustomFeature1" -URL http://contoso.com

Disable Feature

1.#Replace the feature identity and WebApplication url
2.Disable-SPFeature –identity "Rajesh.CustomFeature1" -URL http://contoso.com

Uninstall Feature

1.#Removes the feature definition from the farm.
2.#Replace the feature identity
3.Uninstall-SPFeature –identity "Rajesh.CustomFeature1”

Branding

Changing the master page on all sites at site collection

01.#Replace the site-url with the actual site url
02.$site = Get-SPSite http://contoso.com
03.foreach ($web in $site.AllWebs) {
04.#change the master url accordingly
05.$web.CustomMasterUrl = "/_catalogs/masterpage/V4.master";
06.$web.Update();
07.$web.Dispose();
08.}
09.foreach ($web in $site.AllWebs) {
10.#change the master url accordingly
11.$web.MasterUrl = "/_catalogs/masterpage/v4.master";
12.$web.Update();
13.$web.Dispose();
14.}
15.$site.Dispose();

Set Alternate CSS

1.#Replace the site-url with the actual site url
2.$web = Get-SPWeb http://contoso.com
3.#Replace the css url accordingly
4.$web.AlternateCssUrl = "/Style Library/MyStyles/main.css";
5.$web.AllProperties["__InheritsAlternateCssUrl"] = $True;
6.$web.Update();
7.$web.Dispose();

Set regional setting/locale

01.#Replace the site-url with the actual site url
02.$site = Get-SPSite http://contoso.com
03.foreach ($web in $site.AllWebs) {
04.#Change the Web locale as required, below is for US Eng
05.$web.Locale = 1033;
06.$web.Update();
07.$web.Dispose()
08.}
09.$site.Dispose()

Document Library

Delete document by name from Document Library

01.$site = new-object Microsoft.SharePoint.SPSite("site-url ")
02.$web = $site.openweb()
03.$list=$web.Lists["Document-Library-Name "]
04.$listItems = $list.Items
05.$listItemsTotal = $listItems.Count
06.Write-Host $listItemsTotal
07.for ($x=$listItemsTotal-1;$x -ge 0; $x--)
08.{
09.  if($listItems[$x].name.Contains("file")) # file refers to the    name of the document
10.  {
11.    Write-Host("DELETED: " + $listItems[$x].name)
12.    $listItems[$x].Delete()
13.  }
14.}

Site management

Delete all sites in the given site


01.# completely deletes the specified Web (including all sub sites)
02.function RemoveSPWebRecursively( [Microsoft.SharePoint.SPWeb] $web)
03.{
04.    Write-Debug "Removing site ($($web.Url))..."   
05.    $subwebs = $web.GetSubwebsForCurrentUser()   
06.    foreach($subweb in $subwebs)
07.    {
08.        RemoveSPWebRecursively($subweb)
09.        $subweb.Dispose()
10.    }
11.     
12.    $DebugPreference = "SilentlyContinue"
13.    Remove-SPWeb $web -Confirm:$false
14.    $DebugPreference = "Continue"
15.}
16. 
17.$DebugPreference = "SilentlyContinue"
18.#Replace the site-url with the actual site url
19.$web = Get-SPWeb "http://contoso.com/subsite1"
20.$DebugPreference = "Continue"
21. 
22.If ($web -ne $null)
23.{
24.    RemoveSPWebRecursively $web
25.    $web.Dispose()
26.}