SharePoint Pals
 | Sign In
How to Create Site Collections, SubSite in SharePoint 2013 using PowerShell
by Sathish Nadarajan 16 Mar 2015
Author
SharePoint MVP
Visits   
Today  :   2     Total  :    8197

 

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.

blog comments powered by Disqus

SharePoint Pals

Pals
SharePoint Pals, a community portal for SharePoint developers, Administrators and End Users. Let's join hands and share the point together.
Read this on mobile

Training

Angular Js Training In Chennai
Advanced Angular Js training with real world developer scenarios
Angular Js, Web Api and Ionic for .Net Developers
All in one client side application development for .Net developers
Angular Js For SharePoint Developers
Get ready for the future. Its no more just C#

Get Connected

SharePoint Resources

SharePoint 2013 and 2010 Web Parts
Free Web Parts with Source Code for SharePoint Community




SharePoint 2013 Books and Tutorials
Collection of free SharePoint 2013 books and tutorials (eBooks, pdfs)

Supported By

Contribute your article and be eligible for a one month Free Subscription for Plural Sight. The Author of the most popular New Article (published in the previous month) will be awarded with a Free One month Plural Sight Subscription. Article can be sent to articles@sharepointpals.com in a word document.

Related Resources

Recent Tweets

Twitter January 15, 00:25
How To Enable Target Value And Actual Value In #D3 Gauge Chart https://t.co/VxSi4QnNrC

Twitter January 15, 00:24
How To Open #SharePoint List Hyperlink Column In Modal #Popup Window https://t.co/EQ7HkoZDkX

Twitter January 15, 00:24
Quick Introduction To #Asp.NetCore And It’s Features https://t.co/zAXObHCFpH

Twitter January 15, 00:22
How To Configure #PerformancePoint Services To Use Secure Store In #SharePoint 2013 https://t.co/LEwnUoI7EY

Twitter January 15, 00:21
How To Block Or Disable #Office365 Services https://t.co/Yvp2VPFIRP

Follow us @SharePointPals
Note: For Customization and Configuration, CheckOutRecent Tweets Documentation