Using PowerShell scripts for common SharePoint Server 2010 tasks

One of the great things introduced by SharePoint Server 2010 is PowerShell support. Previously -and by this I mean in SharePoint Server 2007- developers and SharePoint administrators was using Stsadm which a command-line tool for SharePoint 2007. The benefits of using PowerShell over Stsadm is countless includes but not limited to using the powerful capabilities of PowerShell like accessing file system, registry..etc ; that’s why Stsadm support only exists in SharePoint 2010 for backward compatibility but its deprecated.

This is a sample of a PowerShell script that can use to create a site collection based on prompting the user for a url, owner, and a title:

Clear-Host #Clear Screen
Add-PSSnapin Microsoft.Sharepoint.PowerShell -ErrorAction "SilentlyContinue" #adds reference to SharePoint (-ErrorAction "SilentlyContinue") only for running inside Powershell ISE
Write-Host "Starting Sharepoint SPSite Creator..." #outputing a starting string
$url = Read-Host "The URL of the SPSite to create?" #asking for user input
$owner = Read-Host "domain/user?" #asking for user input
$title = Read-Host "Site Title?" #asking for user input
Remove-SPSite -Identity $url -ErrorAction "SilentlyContinue" #remove the site if exists
$site = New-SPSite -Url $url -OwnerAlias $owner -Template STS#1 -Name $title #Create the Site Collection (STS#1) is the blank template
Write-Host $site.RootWeb.Title " has been successfully created with url: " $site.Url #outputing a success string

You can simply write this using notepad and save it with “.ps1” extension and you are good to go, however its better to use PowerShell ISE (PowerShell integrated scripting environment) to get syntax highlighting, debugging..etc

This entry was posted in Software and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s