«

»

Feb 12

Create SharePoint 2010 sites using PowerShell reading an XML file

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.

About Chris McKinley

Chris McKinley has written 26 post on this blog.


Technorati Tags: , , ,

You may also be interested in reading:

  1. 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

  1. Venkat.KL

    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 <<<

  2. Chris McKinley

    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.

  3. Brad Thurber

    Hi – I also would like to Visio files if possible. Thank you – Brad

  4. soegemaskineoptimering

    Hello, I think your article is very informative. I found it via Yahoo. Will definitely come back again

  5. legaspi village condo

    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.

  6. ИгроМАН

    And what will happen if the CML file broken or misspelled? Do not drop the system?

  1. The first steps to SharePoint 2010 PowerShell Heaven « Chris and Dave's SharePoint And Tech Blog

    [...] 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 [...]

  2. uberVU - social comments

    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….

  3. 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

    [...] 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 [...]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>