2

What's the best way to create hundreds of sub sites in a site collection for testing purposes? It doesn't matter what their names are. Doing some research, I found a few scripts that either require that I enter each site's name or that I use an xml file that would still involve me manually entering hundreds of unique names for each sub site. I would just need the sub sites to automatically be named test1, test2, or follow some kind of similar pattern. There has to be some way to leverage the New-SPWeb cmdlet with a for loop maybe? Can it be done without using a config or csv file?

2 Answers 2

5

Below script accepts the array of the sub sites name, But you can amend it instead of passing the SubSIte name arrays just add test(i)...i mean in the for loop instead of checking the array's count, check fix the number of subsite($i <100000)...then susbite url will be like this

$SiteUrl = $SiteCollectionURL + "/test" +$i

. Copy and Paste the code below and save it as CreateSubSite.ps1, see highlighted yellow sections to change configurable values:

Add-PsSnapin Microsoft.SharePoint.PowerShell

## SharePoint DLL 
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") 

#Creating Sub Sites in top site collection.
Write-Output " "
Write-Output "Creating Sub Sites"

$SiteCollectionURL = "http://localhost/sites/SPFix"

$SiteCollectionTemplate = "STS#0" 

$SiteCollectionLanguage = 1033

$SubSites = @("Central Services", "Knowledge Base", "Service Center", "IT", "HR", "Finance")

for($i=0 ; $i -lt $SubSites.count ; $i++)
{
$SiteUrl = ""
$SiteUrl = $SiteCollectionURL + "/" 
$SiteUrl = $SiteUrl += $SubSites[$i]
Write-Output " "
#Write-Output "Creating Site for " += $SubSites[$i]
Write-Output " "
New-SPWeb $SiteUrl -Template $SiteCollectionTemplate -Name $SubSites[$i]  -UseParentTopNav -Language $SiteCollectionLanguage
Write-Output " "
#Write-Output "Site Created for " += $SubSites[$i]
Write-Output " "
}

Remove-PsSnapin Microsoft.SharePoint.PowerShell

http://www.sharepointfix.com/2011/04/powershell-script-to-create-subsites.html

1
  • To test your suggestion, I changed $Subsites from the array to 10. I replaced the line from the script you pasted to this $SiteUrl = $SiteCollectionURL + "/test" +$i I changed the for loop to this for($i=0 ; $i -lt $SubSites ; $i++) It creates 10 sites fine! However, they are named test010, test1, test2 .. up to test 9. I'm missing something in the way I'm naming the sites. I'd like the sites to be consistent, like test1, test2, test3, etc.. any idea what I missed?
    – spex5
    Sep 21, 2014 at 23:52
0

The basic syntax for creating site would be New-SPSite -Url $WebSiteUrl -OwnerAlias $SiteOwner -Name "Store" -Template $webTemplate and creating subsite `New-SPWeb -Url $WebSiteUrl -OwnerAlias $SiteOwner -Name "Store" -Template $webTemplate.

Now for your requirement I think you have to use some kind of loop and call the above syntax for that many times.

Refer this question on syntax site-provisioning-example `

1
  • how can i create a subsite based on a custom site template? can i use the same code ? Oct 17, 2016 at 22:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.