This week I’d like to share a Windows PowerShell script that I was asked to create for a client. The script creates a site collection with the same results as creating a site collection by using Central Administration. Sounds simple, but it’s not… the UI pulls a few tricks that the equivalent PowerShell cmdlet, New-SPSite, does not.

For those of you either trying to learn Windows PowerShell, trying to understand user and group membership management with PowerShell, or those of you just looking to understand more about what happens when you create a site collection, I think this script will prove interesting!

Before I share the script with you, though, let me mention that this week I am in Gold Coast at TechEd Australia. I took a trip down under to learn more about a company I’ve admired for awhile—AvePoint—and to dig more into the broad range of solutions that they offer. I’m delivering a session at the interactive theatre, and I’ll have office hours at the AvePoint booth, so please do be sure to stop by, say hello, and get questions answered!

Now, on to the script!

The following script creates a site collection and top level website with the team site template. The results are the same results as doing so in the Central Administration UI. I’ll comment along the way, and hopefully this script will prove to be a good reference for a number of different learning scenarios. Be sure to run the script using SharePoint 2010 Management Shell, or otherwise ensure that the Microsoft.SharePoint.PowerShell snapin is loaded.

 

The following script creates a site collection and top level website with the team site template. The results are the same results as doing so in the Central Administration UI. I’ll comment along the way, and hopefully this script will prove to be a good reference for a number of different learning scenarios. Be sure to run the script using SharePoint 2010 Management Shell, or otherwise ensure that the Microsoft.SharePoint.PowerShell snapin is loaded.

 # Create a SharePoint 2010 Site Collection and Top-Level Website

 # Using Windows PowerShell with the same results as using Central Administration

 # Including creation of the default Visitors, Members, and Owners Groups

 # Dan Holme - v 1.0 23 Aug 2010

# ---- CONFIGURATION BLOCK ----

# The first section contains the variables you need to specify based on your needs

$url = "http://teams.contoso.com/depts/IT"
$ContentDatabase = "SharePoint_Content_Teams"

    # OK… the content database is not something you are able to specify

    # when creating a site collection in the user interface, but it’s so

    # important and so many people wish it were there, that I’m adding it

   # to this script
$WebsiteName = "Information Technology"
$WebsiteDesc = "Information Technology department team site"
$Template = "STS#0"

    # If you don’t know the names of templates,

    # enter the command GET-SPWebTemplate

# Information about the primary site collection administrator (Owner)

# Note that the information should match what is in Active Directory for

# the username, display name, and email address
$PrimaryLogin = "CONTOSO\LolaJ"
$PrimaryDisplay = "Jacobsen, Lola"
$PrimaryEmail = "Lola.Jacobsen@contoso.com"

# Information about the secondary site collection administrator (Secondary Owner)
$SecondaryLogin = "CONTOSO\AprilM"
$SecondaryDisplay = "Meyer, April"
$SecondaryEmail = "April.Meyer@contoso.com"

 

# Names of the default Members and Viewers groups

# You shouldn’t have to change these unless you’re using a

# default language other than English
$MembersGroup = "$WebsiteName Members"
$ViewersGroup = "Viewers"
# ---- CODE ----

# Following code uses the variables you specified above

# You should not have to change any of the remaining code

# Unless you want to change the functionality of the script itself

 

# Create a site collection and top level website

# The primary and secondary site collection administrators are specified

# In Central Administration, their e-mail addresses are looked up, but

# in PowerShell, you have to specify them or else they end up blank
New-SPSite -Url $url –ContentDatabase $ContentDatabase -Name $WebsiteName –Description $WebsiteDesc  -Template $Template -OwnerAlias $PrimaryLogin –OwnerEmail $PrimaryEmail -SecondaryOwnerAlias $SecondaryLogin -SecondaryEmail $SecondaryEmail

 

# In the user interface, after creating a site collection, the default groups are configured

# automatically. This is not true of the New-SPSite cmdlet, so we have to create the

# default groups (Visitor, Members, and Owners)

$web = Get-SPWeb $url
$web.CreateDefaultAssociatedGroups($PrimaryLogin,$SecondaryLogin,"")

 

# In the user interface, the primary and secondary site collection administrators

# are displayed with their friendly display names as looked up in Active Directory,

# but with PowerShell the users are added to the site collection with their

# display name set to their user name. These lines of code update the display names.

$PrimaryAdmin = Get-SPUser $PrimaryLogin -Web $url
$PrimaryAdmin.Name = $PrimaryDisplay
$PrimaryAdmin.Update()
$SecondaryAdmin = Get-SPUser $SecondaryLogin -Web $url
$SecondaryAdmin.Name = $SecondaryDisplay
$SecondaryAdmin.Update()

# Finish by disposing of the SPWeb object to be a good PowerShell citizen
$web.Dispose()