Friday, April 22, 2011

Powershell script to Backup SharePoint 2010 Farm and Site Collections

To start a full farm backup,
Backup-SPFarm -BackupMethod Full -Directory "Destination-Directory"
-BackupThreads 5

When the above command is executed, a full farm backup will be performed with
five threads to perform the backup. You can specify up to 10 threads. However,
the fewer the backup threads, the easier it is to read the backup log file. You can
also specify "Differential" as the backup method to perform differential backup of
SharePoint farm. By default, this does not show the progress of backup
operation.

To see the progress as backup is performed:
Backup-SPFarm -BackupMethod Full -Directory "Destination-Directory"
-BackupThreads 5 –Verbose

To see a list of all items included in backup:
Backup-SPFarm –ShowTree

To perform a site collection backup:
Backup-SPSite -Identity "http://SharePointFix:101/" -Path "Path to Backup
file"

To perform on site collection backup using SQL snapshots:
Backup-SPSite -Identity "http://SharePointFix:101/" -Path "Path to Backup
file" –UseSqlSnapShot

There is no option in the central administration to perform backup using SQL
snapshots. This can be done using PowerShell only. Also, using SQL Snapshots
is the recommended way to perform a site collection backup. This will allow users
to continue to read / write site collection content while the backup is in progress.

References: Knowledge Base article from http://www.powergui.org/

Thursday, April 21, 2011

Powershell script to create nested subsites within a site collection

Our goal is to create 50 parent subsites and iteratively create 5 nested child subsites underneath each of them. So mathematically it is 50 subsites * 5 nested subsites per parent subsite = 250 nested sub-sites in total.

The powershell scriptlet below accepts Site Collection URL,  Site Collection Template, Site Collection Language, Sub-site Name and a Nested Sub-site name as configurable parameters, it then goes through each of them and creates nested sub-sites for that particular Site Collection.

Instructions:
1. Copy and Paste the code below and save it as CreateNestedSubSites.ps1, see highlighted yellow sections to change configurable values:

Add-PsSnapin Microsoft.SharePoint.PowerShell

## SharePoint DLL
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

#Creating Sub Sites in top site collection.
Write-Output " "
Write-Output "Creating 250 Nested Sub-Sites"

$SiteCollectionURL = "http://localhost/sites/NestedSubSites"

$SiteCollectionTemplate = "BLANKINTERNETCONTAINER#0"

$SiteCollectionLanguage = 1033

$StaplingWeb = "StaplingWeb"

$StaplingNestedSubWeb = "NestedWeb"

## Iterate via 50 Subsites
for($i=0 ; $i -lt 50 ; $i++)
{
    $SiteUrl = ""
    $SubSiteName = ""
    $SiteUrl = $SiteCollectionURL + "/"
    $SubSiteName = $StaplingWeb + $i
    $SiteUrl = $SiteUrl += $SubSiteName

    Write-Host "Creating Sub-Site for " $SubSiteName
    New-SPWeb $SiteUrl -Template $SiteCollectionTemplate -Name $SubSiteName  -UseParentTopNav -Language $SiteCollectionLanguage
    Write-Host "Site Created for " $SubSiteName
   
    ## Create 5 Subsites underneath each of the 50 subsites
    for($j=0 ; $j -lt 5 ; $j++)
    {
        $NestedSubSiteUrl = ""
        $NestedSubSiteName = ""
        $NestedSubSiteName = $StaplingNestedSubWeb + $j
       
        $NestedSubSiteUrl = $SiteUrl + "/"
        $NestedSubSiteUrl = $NestedSubSiteUrl += $NestedSubSiteName
       
        Write-Host "Creating Nested Sub-Site for :" $NestedSubSiteName
        New-SPWeb $NestedSubSiteUrl -Template $SiteCollectionTemplate -Name $NestedSubSiteName  -UseParentTopNav -Language $SiteCollectionLanguage
        Write-Host "Nested Sub-Site created for :" $NestedSubSiteName
    }
}

Remove-PsSnapin Microsoft.SharePoint.PowerShell

2. To automatically run the CreateNestedSubSites.ps1 script as a batch utility, Copy and paste code below and save it with a .bat file extension

cd /d %~dp0
powershell -noexit -file ".\CreateNestedSubSites.ps1" "%CD%"
pause


Run the Script and enjoy :)

Tuesday, April 19, 2011

Powershell Script to create subsites within a site collection

The powershell scriptlet below accepts Site Collection URL,  Site Collection Template, Site Collection Language and Sub Site Names as configurable parameters, it then goes through each subsite array and creates subsites for that particular Site Collection.

Instructions:
1. Copy and Paste the code below and save it as CreateSubSite.ps1, see highlighted yellow sections to change configurable values:

Add-PsSnapin Microsoft.SharePoint.PowerShell

## SharePoint DLL
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

#Creating Sub Sites in top site collection.
Write-Output " "
Write-Output "Creating Sub Sites"

$SiteCollectionURL = "http://localhost/sites/SPFix"

$SiteCollectionTemplate = "STS#0"

$SiteCollectionLanguage = 1033

$SubSites = @("Central Services", "Knowledge Base", "Service Center", "IT", "HR", "Finance")

for($i=0 ; $i -lt $SubSites.count ; $i++)
{
$SiteUrl = ""
$SiteUrl = $SiteCollectionURL + "/"
$SiteUrl = $SiteUrl += $SubSites[$i]
Write-Output " "
#Write-Output "Creating Site for " += $SubSites[$i]
Write-Output " "
New-SPWeb $SiteUrl -Template $SiteCollectionTemplate -Name $SubSites[$i]  -UseParentTopNav -Language $SiteCollectionLanguage
Write-Output " "
#Write-Output "Site Created for " += $SubSites[$i]
Write-Output " "
}

Remove-PsSnapin Microsoft.SharePoint.PowerShell

2. To automatically run the CreateSubSite.ps1 script as a batch utility, Copy and paste code below and save it with a .bat file extension

cd /d %~dp0
powershell -noexit -file ".\CreateSubSite.ps1" "%CD%"
pause

Run the Script and Enjoy :)

Thursday, April 7, 2011

Powershell Script to Delete Site Columns from SharePoint SiteCollection RootWeb


The code below accepts Site Collection URL and Site Columns List as configurable parameters, it then goes through each semi-colon separated Site Columns List and deletes it from the RootWeb of the Site Collection.

Instructions:
1. Copy and Paste the code below and save it as DeleteSiteColumns.ps1, see highlighted sections to change configurable values:

Add-PsSnapin Microsoft.SharePoint.PowerShell
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

set-variable -option constant -name url  -value "http://localhost/" # Specify your Site collection URL
$siteColumnsList = "Display Name Column1;Display Name Column2;Display Name Column3" # Specify a list of Site Column Names to be deleted

$site = new-object Microsoft.SharePoint.SPSite($url)
$array = $siteColumnsList.Split(";")

foreach($colms in $array)
{
try
{
$column = $site.rootweb.Fields[$colms]
$site.rootweb.Fields.Delete($column)
Write-Host $column.Title "deleted successfully."
}
catch [System.Exception]
{
Write-Host $column.Title "deleted failed."
#Best Attempt to Remove Site Columns
}
}

$site.Dispose()

Remove-PsSnapin Microsoft.SharePoint.PowerShell
Echo Finish

2. To automatically run the .ps1 script as a batch utility, Copy and paste code below and save it with a .bat file extension


cd /d %~dp0
powershell -noexit -file ".\DeleteSiteColumns.ps1" "%CD%"
pause


Run the Script and Enjoy :)