A PowerShell script to automate the creation of SharePoint site collections

I must admit it. I’ve never really done anything with PowerShell.
Sure, the Macaw Solutions Factory is largely based on PowerShell, but I usually just use that as-is, or make minor changes to existing scripts.
So starting to work with PowerShell for SharePoint is all new. I read a PowerShell book over the summer holidays to get a little bit of an understanding about PowerShell, but as everyone knows, reading about it is very different from actually using it and doing it hands on. So last week I took my first baby steps in using SharePoint and PowerShell. If you’re a seasoned PowerShell user this post is probably not for you, but if you are, like me, just starting to get familiar with PowerShell you might find the information in here useful.

The goal was to create site collections for everyone in our department. The urls of the site collections are the first names/account names of my team members and of course I didn’t want to type in the same lines 25 times.

  1 $snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
  2 
  3 if ($snapin -eq $null)
  4 {
  5     cd 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG\POWERSHELL\Registration'
  6     .\SharePoint.ps1
  7 }
  8 
  9 $users = "john", "paul", "mirjam"
 10 
 11 foreach ($user in $users)
 12 {
 13     New-SPSite -Url "http://iws.macaw.nl/sites/$user" -OwnerAlias "DOMAIN\$user" -Name "Portal" -Template "STS#0" 
 14     Write-Output "Site created for $user" 
 15 } 

Before we can use the SharePoint PowerShell cmdlets we need to load the Microsoft.SharePoint.PowerShell snapin. We can accomplish this by loading the SharePoint.ps1 file or by adding the directly. If you try to add a snapin that is already added PowerShell will throw an error telling you that. Even though the script will continue after throwing the error without problems I didn’t like seeing the error, so I build in a test that checks if the snapin was already added. There might be a better way to test this, but I couldn’t find one, so I ended up filtering the collection of registered snapins on snapins with the name “Microsoft.SharePoint.PowerShell”. If the resulting collection contains an item (has more than 0 items in it) the snapin is already registered.

[Update]: I adjusted the “if” statement according to the statement Leon posted in the comments (thanks Leon!). This test first gets the snapin where the name of the snapin equals Microsoft.SharePoint.PowerShell. It adds the results to the @snapin variable. Next the if statement checks whether there is actually a snapin in the @snapin variable, or whether it is NULL. As you can see in Leon’s script you can also choose to add the Microsoft.SharePoint.PowerShell snapin directly, but there is some other magic in the SharePoint.ps1 file and since I didn’t know what that was for I decided to just load the .ps1 file completely.

Next I define an array of users. There are far more spiffy ways to do this, like for instance reading the names from a file, but I decided that this would do for now.

Now we can loop through all users in the array and create a new site for each user. The urls of each site are http://iws.macaw.nl/sites/[[username]], so the url of my site is http://iws.macaw.nl/sites/mirjam. Note how I use double quotes around the url. Because I use double quotes PowerShell replaces the variable name $user by its contents and uses that in the string. If you use single quotes PowerShell will leave the string alone and the url would be http://iws.macaw.nl/sites/$user. The same principle is used for the owneralias and the output stating that the site for this user was created.

I hope this helps people. If you have improvements or additions to the script feel free to post them in the comments!

Print | posted @ Thursday, December 17, 2009 3:06 PM

Comments on this entry:

Gravatar # Slightly different approach
by Leon Zandman at 12/17/2009 7:03 PM

The SharePoint 2010 Ignite Hands-on Labs contain PowerShell scripts that help you set up the environment for the lab. Those scripts take a slightly different approach. They don't use the Powershell.ps1 from the CONFIG directory, but they call Add-PSSnapin directly. I don't know which method is better. The script from the CONFIG directory also sets some thread option, but that might be unnecessary. This is the method used in those scripts:

# check to ensure Microsoft.SharePoint.PowerShell is loaded
$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
if ($snapin -eq $null) {
Write-Host "Loading SharePoint Powershell Snapin"
Add-PSSnapin "Microsoft.SharePoint.Powershell"
}


And as a bonus they also show how to delete a site collection:

# delete any existing site found at target URL
$SiteUrl = "http://twiki/sites/Leon"
$targetUrl = Get-SPSite | Where-Object {$_.Url -eq $SiteUrl}
if ($targetUrl -ne $null) {
Write-Host "Deleting existing site at" $SiteUrl
Remove-SPSite -Identity $SiteUrl -Confirm:$false
}
  
Gravatar # re: A PowerShell script to automate the creation of SharePoint site collections
by sam at 12/18/2009 7:20 AM

thanks for posting
  
Gravatar # re: A PowerShell script to automate the creation of SharePoint site collections
by VincentH at 12/18/2009 7:55 AM

Good to see you've taken the PowerShell plunge Mirjam!

I hope more devs will be inspired by your example to master PowerShell as their second language (C# being the first for many).

I'm sure there are many people out there who could create great Factory extensions using PowerShell.
  
Gravatar # re: A PowerShell script to automate the creation of SharePoint site collections
by mirjam at 12/21/2009 2:27 PM

Hi Leon,

Thanks for the additions, I like the fi statement you're using better, so I'll adjust the post!

Mirjam
  
Gravatar # re: A PowerShell script to automate the creation of SharePoint site collections
by Kiki at 4/30/2010 8:37 AM

Nice post although i think there can be a better coding.
  
Gravatar # agence internet
by agence internet at 6/27/2010 8:14 AM

CSS3 is the new kid in the stylesheet family. It offers exciting new possibilities to create an impact with your designs, allows you to use more diverse style sheets for a variety of occasions and lots more.CSS3 supports more color and a wider range of color definitions. The new colors that CSS3 support are HSL, CMYK, HSLA and RGBA. This powerpoint explains in details the CSS3 colors and how use them
  
Gravatar #  the creation of SharePoint site collections
by hotfile download at 7/31/2010 2:47 AM

its very nice creation. congrats for your hardwork..
  

Your comment:

Title:
Name:
Email:
Website:
 
Italic Underline Blockquote Hyperlink
 
 
Please add 8 and 3 and type the answer here: