Create Site Collections in SharePoint Server using PowerShell and an XML file
We have so many different ways to achieve something using PowerShell.
In this blog post, we’ll have a look at how we can create Site Collections in SharePoint Server using an XML file and PowerShell (of course!)
Open your favourite editors (I’m using Notepad++ and Visual Studio Code) and let’s get started!
Create the XML file
XML (eXtensible Markup Language) files have their syntax, so it’s important to be familiar with it.
If you’re not, have a look at the Introduction to XML provided by w3chools.com, it may help you to get started 🙂
For the purpose of this post, we are going to create 2x Site Collections with 2 different templates, all in the same XML file.
Below, I tried to give you some information with comments (in green for first block) and also delimiting the sections for more clarity in case you’re not familiar with XML file.
We don’t have to “space” the Site Collections sections, but… again, just for clarity.
Note that the tags “title“, “url“, etc… will be the parameters we’ll use in PowerShell.
You can name them whatever you wish, as long as you reference them correctly in your script. But I think it’s best to be explicit, keep it simple, and know what they entail 😉
If you need to find the list of templates (i.e.: WIKI#0), simply run Get-SPWebTemplate.
Create the PowerShell script
Now that we have our XML file, let’s move on to the PowerShell script.
#XML file
[xml]$XMLfile = Get-Content "C:\users\$env:USERNAME\Desktop\SiteCollections.xml"
#Loop through each Site Collection + create
foreach ($SC in $XMLfile.SiteCollections.Site) {
New-SPSite -Name $SC.title -Url $SC.url -Description $SC.description -OwnerAlias $SC.owner
}
In the foreach loop, we need to “drill down” to the section where we are referencing the parameters for the Site Collections, hence the $XMLfile.SiteCollections.Site
Now run the script, and let’s check the results.
⚠️ Don’t forget to add the SharePoint snapin if you’re not on the SharePoint Management Shell
Check the results
We can verify the Site Collections have been created by logging into SharePoint Central Admin, or by PowerShell.
SharePoint Central Admin:
- Go to Application Management > View all site collections
- Select your Web Application
- Observe the results
PowerShell:
Get-SPSite -WebApplication <YOUR_WEB_APPLICATION_URL>