Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
How to Create Subsites Using Powershell in Sharepoint 2013
WhatsApp
Gaurav Arora
10y
37.6k
0
2
100
Article
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
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
input
>
<
WebApplication
>
<
URL
>
http://spdev-2013:2020
</
URL
>
</
WebApplication
>
<
SiteCollection
>
<
Name
>
company
</
Name
>
<
Title
>
Company Website
</
Title
>
<
TemplateCode
>
BLANKINTERNET#0
</
TemplateCode
>
<
PrimaryAdmin
>
WINGTIP\SPFarmAcc
</
PrimaryAdmin
>
<
SubSites
>
<
Site
>
<
Name
>
Media & Entertainment
</
Name
>
<
Url
>
media-entertainment
</
Url
>
</
Site
>
<
Site
>
<
Name
>
Travel & Hospitality
</
Name
>
<
Url
>
travel-hospitality
</
Url
>
</
Site
>
<
Site
>
<
Name
>
Online Retail
</
Name
>
<
Url
>
online-retail
</
Url
>
</
Site
>
<
Site
>
<
Name
>
Healthcare & Life Sciences
</
Name
>
<
Url
>
healthcare-life-sciences
</
Url
>
</
Site
>
<
Site
>
<
Name
>
Product Engineering Services
</
Name
>
<
Url
>
product-engineering-services
</
Url
>
</
Site
>
<
Site
>
<
Name
>
Enterprise Business Solutions
</
Name
>
<
Url
>
enterprise-business-solution
</
Url
>
</
Site
>
<
Site
>
<
Name
>
Elearning
</
Name
>
<
Url
>
elearning
</
Url
>
</
Site
>
<
Site
>
<
Name
>
Support Services
</
Name
>
<
Url
>
support-services
</
Url
>
</
Site
>
<
Site
>
<
Name
>
UX Design
</
Name
>
<
Url
>
ux-design
</
Url
>
</
Site
>
<
Site
>
<
Name
>
Documentation
</
Name
>
<
Url
>
documentation
</
Url
>
</
Site
>
<
Site
>
<
Name
>
Cloud Computing
</
Name
>
<
Url
>
cloud-computing
</
Url
>
</
Site
>
<
Site
>
<
Name
>
Business Intelligence
</
Name
>
<
Url
>
business-intelligence
</
Url
>
</
Site
>
<
Site
>
<
Name
>
Enterprise Mobility
</
Name
>
<
Url
>
enterprise-mobility
</
Url
>
</
Site
>
<
Site
>
<
Name
>
CRM
</
Name
>
<
Url
>
crm
</
Url
>
</
Site
>
<
Site
>
<
Name
>
E-commerce
</
Name
>
<
Url
>
e-commerce
</
Url
>
</
Site
>
<
Site
>
<
Name
>
Supply Chain Management
</
Name
>
<
Url
>
scm
</
Url
>
</
Site
>
<
Site
>
<
Name
>
ECM
</
Name
>
<
Url
>
ecm
</
Url
>
</
Site
>
<
Site
>
<
Name
>
Careers
</
Name
>
<
Url
>
careers
</
Url
>
</
Site
>
<
Site
>
<
Name
>
News
</
Name
>
<
Url
>
news
</
Url
>
</
Site
>
<
Site
>
<
Name
>
Global
</
Name
>
<
Url
>
global
</
Url
>
</
Site
>
</
SubSites
>
</
SiteCollection
>
<
ContentDB
>
<
Name
>
WSS_Content_Dummy
</
Name
>
<
ServerName
>
SPDev-2013
</
ServerName
>
</
ContentDB
>
</
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
Add-PSSnapin Microsoft.SharePoint.Powershell
# pauses like regular cmd prompt
function
Pause ($Message=
"Press any key to continue..."
)
{
Write-Host -NoNewLine $Message
$
null
= $Host.UI.RawUI.ReadKey(
"NoEcho,IncludeKeyDown"
)
Write-Host
""
}
#log the operation details in text file
$logfile =
".\Create_Site_Collection_Log_$(get-date -format `"
yyyyMMdd_hhmmsstt`
").txt"
function
log($string, $color)
{
if
($Color -eq $
null
) {$color =
"white"
}
write-host
"[$(Get-Date -Format T)] $string"
-foregroundcolor $color
"[$(Get-Date -Format T)] $string"
| out-file -Filepath $logfile -append
}
#get the XML file
[System.Xml.XmlDocument] $XmlDoc =
new
-object System.Xml.XmlDocument
$file =
resolve-path(
".\input.xml"
)
if
(!$file)
{
log
"Could not find the configuration file specified."
red
Break
}
$XmlDoc = [xml](Get-Content $file)
#Get the site template code
$SiteTemplate = $XmlDoc.input.SiteCollection.TemplateCode
$siteCollectionUrl = $XmlDoc.input.SiteCollection.Url
# clear some space
Write-Host
""
Write-Host
""
Write-Host
""
# add solution
Write-Host
"Checking if site:: $newSiteUrl already exist.."
$targetUrl = Get-SPSite -Limit All | Where-Object {$_.Url -eq $siteCollectionUrl}
if
($targetUrl -ne $
null
) {
log
"The site : $newSiteUrl is present."
if
($error.Count -eq 0)
{
log
"Now Creating Subsites.."
$subSites = $XmlDoc.input.SiteCollection.SubSites
foreach($subsiteNode
in
$subSites.Site)
{
$SubSiteName = $subsiteNode.Name
$SubSiteUrl = $newSiteUrl +
"/"
+ $subsiteNode.Url
log
"Creating new subsite : $SubSiteUrl"
$NewSubSite = New-SPWeb -Url $SubSiteUrl -Template $SiteTemplate -Name $SubSiteName
if
($error.Count -eq 0)
{
log
"SubSite Created Successfully..!!"
log
"Breaking Inheritance On A Subsite"
$NewSubSite.BreakRoleInheritance($
true
,$
true
)
$NewSubSite.Update()
}
else
{
log
"SubSite Creation failed"
red
log $error
}
}
}
else
{
log
"Site Collection Creation failed"
red
log $error
}
}
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
Powershell
Sharepoint 2013
Site Collection and Subsites using Powershell
Up Next
Ebook Download
View all
SharePoint Framework (SPFx) A Developers Guide
Read by 11.1k people
Download Now!
Learn
View all
Membership not found