Just a quick post to show how easy it is to create some sites in SharePoint 2010 using PowerShell by reading an XML file. I’m sure there are some powershell experts out there that can improve the code, but if you need to create a revision gateway during lunch this is the place to start.
Lets start by opening PowerShell with modules on the sharepoint server.
Create a sample xml file:
"<Setup> <Sites> <TopSiteName>Site1</TopSiteName> </Sites> <Sites> <TopSiteName>Site2</TopSiteName> <SubSiteName>Subsite2a</SubSiteName> <SubSiteName>Subsite2b</SubSiteName> </Sites> <Sites> <TopSiteName>Site3</TopSiteName> <SubSiteName>Subsite3a</SubSiteName> <SubSiteName>Subsite3b</SubSiteName> <SubSiteName>Subsite3c</SubSiteName> </Sites> </Setup>" | out-file sample.xml
Now we have some data to work with we can create the sites. Three top sites will be created with 2 and 3 subsites below Site2 and Site3. They can be created using:
[xml]$s = get-content sample.xml
foreach ($e in $s.Setup.Sites){
$v = $e.TopSiteName
$b = $e.SubSiteName
new-SPWeb http://sp2010rc/$v -template "STS#0" -addtotopnav -useparenttopnav -name $v
if($b.Length -gt 0) {
foreach ($b in $b){
new-SPWeb http://sp2010rc/$v/$b -template "STS#0" -name $b -AddToQuickLaunch -useparenttopnav
}
}
}
The bit that creates the site is the new-SPWeb command. For information about the command switches run:
get-help new-SPWeb -full
It’s as easy as that! You will now have some SharePoint 2010 sites created in a matter of seconds.
To remove sites you can just use the remove-SPWeb command. This can be scripted in the same way:
[xml]$s = get-content sample.xml
foreach ($e in $s.Setup.Sites){
$v = $e.TopSiteName
$b = $e.SubTopSiteName
if($b.Length -gt 0) {
foreach ($b in $b){
remove-SPWeb http://sp2010rc/$v/$b
}
}
remove-SPWeb http://sp2010rc/$v
}
The xml file can then be deleted with “Remove-Item sample.xml”
If you really wan’t to get stuck in run “Get-Command -module Microsoft.SharePoint.PowerShell” This will output all the cmdlets that you can use with PowerShell to do great things with SharePoint 2010 combine this with get-help and there is no limit to where you can go!!
So there we have it; One XML file with a site structure creating all the SharePoint 2010 sites in just a few seconds!
Happy site creating. Comments and experiences welcome.
You may also be interested in reading:
- Teaching an old dog new tricks. Using PowerShell to make life better. PowerShell has been around for a while now and I...







6 comments
3 pings
Venkat.KL
February 12, 2010 at 5:52 pm (UTC 1) Link to this comment
Hi,
When I tried the above script, its giving error like;
Cannot convert value “System.Object[]” to type “System.Xml.XmlDocument”. Error: “Data at the root level is invalid. Lin
e 1, position 1.”
At C:\Scripts\SiteCreatingUsingPowerShell.ps1:1 char:8
+ [xml]$s <<<< = get-content sample.xml
+ CategoryInfo : MetadataError: (:) [], ArgumentTransformationMetadataException
+ FullyQualifiedErrorId : RuntimeException
The term 'new-SPWeb' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the s
pelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Scripts\SiteCreatingUsingPowerShell.ps1:5 char:10
+ new-SPWeb <<<
Chris McKinley
February 13, 2010 at 11:12 am (UTC 1) Link to this comment
You need to run the script on the SharePoint server. Also make sure you start powershell with the modules loaded, or open powershell via the sharepoint start menu item.
Brad Thurber
February 15, 2010 at 6:11 pm (UTC 1) Link to this comment
Hi – I also would like to Visio files if possible. Thank you – Brad
soegemaskineoptimering
June 12, 2010 at 12:37 pm (UTC 1) Link to this comment
Hello, I think your article is very informative. I found it via Yahoo. Will definitely come back again
legaspi village condo
June 13, 2010 at 12:01 am (UTC 1) Link to this comment
This could be the earliest time I have commented right here and I ought to say you give genuine, and high quality facts for bloggers! Fantastic job.
ИгроМАН
September 23, 2011 at 9:45 am (UTC 1) Link to this comment
And what will happen if the CML file broken or misspelled? Do not drop the system?
The first steps to SharePoint 2010 PowerShell Heaven « Chris and Dave's SharePoint And Tech Blog
February 13, 2010 at 4:09 pm (UTC 1) Link to this comment
[...] to SharePoint 2010 PowerShell Heaven 13 02 2010 As Chris showed in his post last week http://sharepointedutech.com/2010/02/12/creat-sharepoint-2010-sites-using-powershell-reading-an-xml-… PowerShell in SharePoint 2010 is going to be a very useful tool to master over 500 commands as [...]
uberVU - social comments
February 15, 2010 at 9:07 am (UTC 1) Link to this comment
Social comments and analytics for this post…
This post was mentioned on Twitter by crmckinley: New Blogpost: Read an XML file to create SharePoint 2010 sites using Powershell – http://tinyurl.com/ycgzhpx . Please RT….
Tweets that mention Create SharePoint 2010 sites using PowerShell reading an XML file - SharePointEduTech, SharePointEduTech Create SharePoint 2010 sites using PowerShell reading an XML file - SharePointEduTech -- Topsy.com
January 27, 2011 at 2:26 pm (UTC 1) Link to this comment
[...] This post was mentioned on Twitter by Geoff Varosky, Talbott Crowell. Talbott Crowell said: RT @gvaro: Create SharePoint 2010 sites using PowerShell reading an XML file – http://tinyurl.com/2cpg6xm #SPEduTech [...]