Showing posts with label Site Collection Creation. Show all posts
Showing posts with label Site Collection Creation. Show all posts

Thursday, December 20, 2012

PowerShell Script to Create Site Collections for SharePoint 2013

After setting up my SharePoint 2013 farm with Host Name Site Collections I wanted a quick easy way to create a new Site Collection, since you can't do it properly through Central Admin.

So I sat down and wrote this PowerShell script that will prompt the user for all necessary information to create a new SC with an option of creating a new Database.

To run you can copy the below script and paste it into a ps1 file.


Add-PSSnapin Microsoft.SharePoint.Powershell
Write-Host "This script can be used to create Site Collections for SharePoint 2013"
$webApp = Get-SPWebApplication | Where-Object {$_.DisplayName -eq "SharePoint HNSC Host - 80"}
IF ($webApp){
 $ans = Read-Host "I found a HNSC Host Web App, would you like me to use it?. Y or N"
 
}
ELSE {
 $ans = 'N'
}
IF ($ans -eq 'N' -or $ans -eq 'n') {
 $webApp = $null
    Write-Host "Here are the web applications I see on your farm"
    Get-SPWebApplication
 while (!$webApp) {
  $webAppName = Read-Host "What is the DisplayName of the Web Application you'd like to use? "
  $webApp = Get-SPWebApplication | Where-Object {$_.DisplayName -eq $webAppName}
  IF (!$webApp) {Write-Host "I didn't find a Web Application with that name. Be sure to enter the full name exactly as it's listed above."}
 }
}
$siteName = Read-Host "Site Name"
$siteDescription = Read-Host "Site Description"
$siteUrl = Read-Host "Site Url (ie http://site.sp2013.com)"
$seeTemplates = Read-Host "Would you like to see a list of templates codes? Y or N"
IF ($seeTemplates -eq 'y' -or $seeTemplates -eq 'Y') {Get-SPWebTemplate}
$siteTemplate = Read-Host "Site Template (ie Blank - STS#0, Team STS#1, etc)"
$siteAdmin = Read-Host "Site Admin (ie ms\devspadmin)"
$createDB = Read-Host "Finally would you like to create a new Content Database for this Site Collection?"
$db = $null
IF($createDB -eq 'Y' -or $createDB -eq 'y') {
    $dbName = Read-Host "Database Name"
    Write-Host "Creating Database..."
    $db = New-SPContentDatabase -name $dbName -webApplication $webApp
}
ELSE {
    Get-SPContentDatabase -webapplication $webApp
    while(!$db) {
        $dbName = Read-Host "What Content Database would you like to use?"
        $db = Get-SPContentDatabase -Identity $dbName
        IF (!$db) {Write-Host "I didn't find a Content Database with that name. Be sure to enter the full name exactly as it's listed above." }
    }
}
Write-Host "Creating Site Collection..."
New-SPSite -Name $siteName -Url $siteUrl –HostHeaderWebApplication $webApp -Template $siteTemplate -OwnerAlias $siteAdmin -ContentDatabase $db -Description $siteDescription
Write-Host "Script Complete"

 
- Owen Runnals
SharePoint Practice Manager @ General Networks Corp