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
#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
Uninstall-SPSolution -Identity Sample.wsp -WebApplication http://contoso.com –GACDeployment
Enable Feature
#Replace the feature identity and WebApplication url
Enable-SPFeature –identity "Rajesh.CustomFeature1" -URL http://contoso.com
Disable Feature
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
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.
10.
11.
$web.MasterUrl = "/_catalogs/masterpage/v4.master";
12.
13.
14.
15.
$site.Dispose();
Set Alternate CSS
$web = Get-SPWeb http://contoso.com
#Replace the css url accordingly
$web.AlternateCssUrl = "/Style Library/MyStyles/main.css";
5.
$web.AllProperties["__InheritsAlternateCssUrl"] = $True;
6.
7.
Set regional setting/locale
#Change the Web locale as required, below is for US Eng
$web.Locale = 1033;
$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"
16.
17.
18.
19.
$web = Get-SPWeb "http://contoso.com/subsite1"
20.
21.
22.
If ($web -ne $null)
23.
24.
RemoveSPWebRecursively $web
25.
26.