How to Create Subsites Using Powershell in Sharepoint 2013

If you want to create multiple subsites in a site collection and the number of subsites is more than two then why create subsites from the SharePoint UI? It takes too much time, instead we can use Powershell (cmdlets) and create multiple subsites in a site collection.
  • First, we create the XML structure for creating subsites in a site collection.
  • Open Notepad++ and create one XML file for the site structure.

    input.xml
    1. <?xml version="1.0" encoding="utf-8"?>    
    2. <input>     
    3.     <WebApplication>    
    4.         <URL>http://spdev-2013:2020</URL>    
    5.     </WebApplication>    
    6.     <SiteCollection>    
    7.         <Name>company</Name>    
    8.         <Title>Company Website</Title>    
    9.         <TemplateCode>BLANKINTERNET#0</TemplateCode>    
    10.         <PrimaryAdmin>WINGTIP\SPFarmAcc</PrimaryAdmin>      
    11.         <SubSites>    
    12.             <Site>    
    13.                 <Name>Media & Entertainment</Name>    
    14.                 <Url>media-entertainment</Url>    
    15.             </Site>    
    16.             <Site>    
    17.                 <Name>Travel & Hospitality</Name>    
    18.                 <Url>travel-hospitality</Url>    
    19.             </Site>    
    20.             <Site>    
    21.                 <Name>Online Retail</Name>    
    22.                 <Url>online-retail</Url>    
    23.             </Site>    
    24.             <Site>    
    25.                 <Name>Healthcare & Life Sciences</Name>    
    26.                 <Url>healthcare-life-sciences</Url>    
    27.             </Site>    
    28.             <Site>    
    29.                 <Name>Product Engineering Services</Name>    
    30.                 <Url>product-engineering-services</Url>    
    31.             </Site>    
    32.             <Site>    
    33.                 <Name>Enterprise Business Solutions</Name>    
    34.                 <Url>enterprise-business-solution</Url>    
    35.             </Site>    
    36.             <Site>    
    37.                 <Name>Elearning</Name>    
    38.                 <Url>elearning</Url>    
    39.             </Site>    
    40.             <Site>    
    41.                 <Name>Support Services</Name>    
    42.                 <Url>support-services</Url>    
    43.             </Site>    
    44.             <Site>    
    45.                 <Name>UX Design</Name>    
    46.                 <Url>ux-design</Url>    
    47.             </Site>    
    48.             <Site>    
    49.                 <Name>Documentation</Name>    
    50.                 <Url>documentation</Url>    
    51.             </Site>    
    52.             <Site>    
    53.                 <Name>Cloud Computing</Name>    
    54.                 <Url>cloud-computing</Url>    
    55.             </Site>    
    56.             <Site>    
    57.                 <Name>Business Intelligence</Name>    
    58.                 <Url>business-intelligence</Url>    
    59.             </Site>    
    60.             <Site>    
    61.                 <Name>Enterprise Mobility</Name>    
    62.                 <Url>enterprise-mobility</Url>    
    63.             </Site>    
    64.             <Site>    
    65.                 <Name>CRM</Name>    
    66.                 <Url>crm</Url>    
    67.             </Site>    
    68.             <Site>    
    69.                 <Name>E-commerce</Name>    
    70.                 <Url>e-commerce</Url>    
    71.             </Site>    
    72.             <Site>    
    73.                 <Name>Supply Chain Management</Name>    
    74.                 <Url>scm</Url>    
    75.             </Site>    
    76.             <Site>    
    77.                 <Name>ECM</Name>    
    78.                 <Url>ecm</Url>    
    79.             </Site>       
    80.             <Site>    
    81.                 <Name>Careers</Name>    
    82.                 <Url>careers</Url>    
    83.             </Site>    
    84.             <Site>    
    85.                 <Name>News</Name>    
    86.                 <Url>news</Url>    
    87.             </Site>    
    88.             <Site>    
    89.                 <Name>Global</Name>    
    90.                 <Url>global</Url>    
    91.             </Site>    
    92.         </SubSites>    
    93.     </SiteCollection>    
    94.     <ContentDB>    
    95.         <Name>WSS_Content_Dummy</Name>    
    96.         <ServerName>SPDev-2013</ServerName>    
    97.     </ContentDB>    
    98.         
    99. </input>    
  • After the XML sturucture, now we need to create a Powershell file.

  • For the Powershell file, create another file in Notepad++ and write the following Powershell script in that, save the file as whatever name you want with the extension .ps1.

    CreateSubsite.ps1
    1. Add-PSSnapin Microsoft.SharePoint.Powershell    
    2.   
    3.   
    4. # pauses like regular cmd prompt    
    5. function Pause ($Message="Press any key to continue...")    
    6. {    
    7.     Write-Host -NoNewLine $Message    
    8.     $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")    
    9.     Write-Host ""    
    10. }    
    11.   
    12. #log the operation details in text file    
    13. $logfile = ".\Create_Site_Collection_Log_$(get-date -format `"yyyyMMdd_hhmmsstt`").txt"    
    14. function log($string, $color)    
    15. {    
    16.    if ($Color -eq $null) {$color = "white"}    
    17.    write-host "[$(Get-Date -Format T)] $string" -foregroundcolor $color    
    18.    "[$(Get-Date -Format T)] $string" | out-file -Filepath $logfile -append    
    19. }    
    20.   
    21.   
    22. #get the XML file    
    23. [System.Xml.XmlDocument] $XmlDoc = new-object System.Xml.XmlDocument    
    24. $file = resolve-path(".\input.xml")    
    25. if (!$file)    
    26. {    
    27.     log "Could not find the configuration file specified." red    
    28.     Break    
    29. }    
    30.     
    31. $XmlDoc = [xml](Get-Content $file)    
    32.   
    33. #Get the site template code    
    34. $SiteTemplate = $XmlDoc.input.SiteCollection.TemplateCode    
    35.     
    36. $siteCollectionUrl = $XmlDoc.input.SiteCollection.Url    
    37.   
    38. # clear some space    
    39. Write-Host ""    
    40. Write-Host ""    
    41. Write-Host ""    
    42.   
    43.   
    44. # add solution    
    45. Write-Host "Checking if site:: $newSiteUrl already exist.."    
    46. $targetUrl = Get-SPSite -Limit All | Where-Object {$_.Url -eq $siteCollectionUrl}    
    47. if ($targetUrl -ne $null) {    
    48.         
    49.     log "The site : $newSiteUrl is present."    
    50.         
    51.     if($error.Count -eq 0)    
    52.     {    
    53.         log "Now Creating Subsites.."    
    54.             
    55.         $subSites = $XmlDoc.input.SiteCollection.SubSites    
    56.         foreach($subsiteNode in $subSites.Site)    
    57.         {    
    58.             $SubSiteName = $subsiteNode.Name    
    59.                 
    60.             $SubSiteUrl = $newSiteUrl + "/" + $subsiteNode.Url    
    61.                 
    62.             log "Creating new subsite : $SubSiteUrl"    
    63.                 
    64.             $NewSubSite = New-SPWeb -Url $SubSiteUrl -Template $SiteTemplate -Name $SubSiteName    
    65.                 
    66.                 if($error.Count -eq 0)    
    67.                     {    
    68.                         log "SubSite Created Successfully..!!"  
    69.                         log "Breaking Inheritance On A Subsite"    
    70.                         $NewSubSite.BreakRoleInheritance($true,$true)    
    71.                         $NewSubSite.Update()    
    72.                     }    
    73.                 else    
    74.                     {       
    75.                         log "SubSite Creation failed" red    
    76.                         log $error    
    77.                     }    
    78.         }               
    79.     }    
    80.         else    
    81.         {       
    82.             log "Site Collection Creation failed" red    
    83.             log $error    
    84.         }       
    85. }    
    86. pause    

  • Read the highlighted part in the code block of the Powershell file, $file = resolve-path(".\input.xml") , this line shows your Powershell script mapped with your XML structure.

  • After doing these two files, put both files in one folder with whatever name you want .

  • Now run the Powershell script in the SharePoint 2013 Management Shell.

  • Write the exact path of your Powershell file in the Management shell. Suppose you put your Powershell file and XML file in the C drive folder.

  • Then you write:
    C:\powershell> .\CreateSubsite.ps1