How to Create Site Collections, SubSite in SharePoint 2013 using PowerShell

Sathish Nadarajan
 
Solution Architect
March 16, 2015
 
Rate this article
 
Views
13325

 

In Many deployments, the first thing we need to make sure is whether the site collections were created appropriately on the Customers environment or not. Definitely every one of us would have faced this atleast once in our development phase.

So, it is always better to include the Site Collections, Subsite Creations Scripts as well along with the Deployment package. The input for this, in this article, I am reading from the XML File. Based on our convenient, we can modify the script.

The script is as follows.

 $LogTime = Get-Date -Format yyyy-MM-dd_hh-mm
 $LogFile = ".CreateSiteCollections-$LogTime.rtf"
 
 
 cls
 
 
 ##================================================================================================
 ## Description	: Create Site Collections
 ## Author		: Sathish Nadarajan
 ## Date			: 10-Dec-2014
 ##================================================================================================
 
 
  
 $Host.UI.RawUI.WindowTitle = "-- Create Site Collectionos --"
 
 $StartDate = Get-Date
 Write-Host -ForegroundColor White "------------------------------------"
 Write-Host -ForegroundColor White "| Create Site Collections |"
 Write-Host -ForegroundColor White "| Started on: $StartDate |"
 Write-Host -ForegroundColor White "------------------------------------"
 
 
 
 #start-transcript $logfile
 
 ################################################### Add SharePoint PowerShell Snapin   ######################################
 
 
 if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) {
     Add-PSSnapin Microsoft.SharePoint.Powershell
 }
 
 
 ########################################################### End of PowerShell Snapin ########################################
 
 ########################################################### Set the Exxecution Path #########################################
 
 $scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
 Set-Location $scriptBase
 
 
 
 #Read the data from the Xml file
 $SiteCollectionInfo = [xml](Get-Content "01.CreateSiteCollections.xml")
 #Get the Web Application URL
 $WebApplicationURL = $SiteCollectionInfo.SitesInfo.WebApplicationURL
 
 foreach($Site in $SiteCollectionInfo.SitesInfo.Site)
     {            
         Write-Host -ForegroundColor Yellow "Going to Create the Site Collection :- " $Site.Name
         Write-Host "The Details are as follows" -ForegroundColor Yellow
         Write-Host -ForegroundColor Yellow "URL :- " $Site.URL " Owner Alias :- " $Site.OwnerAlias " Name :- " $Site.Name " Template :- " $Site.Template
         
         $template = Get-SPWebTemplate -Identity $Site.Template
         New-SPSite $Site.URL -OwnerAlias $Site.OwnerAlias -Name $Site.Name -Template $template
 
         Write-Host -ForegroundColor Green "Site Collection Successfully Created :- " $Site.Name
 
         foreach($SubSite in $Site.SubSites.SubSite)
         {
             Write-Host -ForegroundColor Yellow "Going to Create the Sub Site :- " $SubSite.Name
             Write-Host "The Details are as follows" -ForegroundColor Yellow
             Write-Host -ForegroundColor Yellow "URL :- " $SubSite.URL " Name :- " $SubSite.Name " Template :- " $SubSite.Template
 
             $subSiteTemplate = Get-SPWebTemplate -Identity $SubSite.Template
             New-SPWeb $SubSite.URL -Template $subSitetemplate -Name $SubSite.Name 
             
             Write-Host -ForegroundColor Green "SubSite Successfully Created :- " $SubSite.Name
 
             foreach($L2SubSite in $SubSite.L2SubSite)
             {
                 Write-Host -ForegroundColor Yellow "Going to Create the Level Two Sub Site :- " $L2SubSite.Name
                 Write-Host "The Details are as follows" -ForegroundColor Yellow
                 Write-Host -ForegroundColor Yellow "URL :- " $L2SubSite.URL " Name :- " $L2SubSite.Name " Template :- " $L2SubSite.Template
 
                 $L2subSiteTemplate = Get-SPWebTemplate -Identity $L2SubSite.Template
                 New-SPWeb $L2SubSite.URL -Template $L2subSiteTemplate -Name $L2SubSite.Name 
 
                 Write-Host -ForegroundColor Green "Level 2 SubSite Successfully Created :- " $L2SubSite.Name
 
             }
         }
 
     }   
 
 
 Write-Host "Script Executed Successfully....." -ForegroundColor Green
 
 #Stop-Transcript
 

DOWNLOAD HERE

Happy Coding.

Sathish Nadarajan.

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment