I want to create site collection and subsites using PowerShell script. It needs to take site name, site template name and owner details as parameters.

I am new to PowerShell. Can anyone please help on this?


Use the following script for creating sites :

$webTemplates = Get-SPWebTemplate  

    foreach ($webTemplate in $webTemplates) 
        # Write-Host "--> " $webTemplate.Name    
        if($webTemplate.Name -like "*CSStorefrontSite#0")
            Write-Host "Creating Root Site Collection " $webTemplate.Name
            New-SPSite -Url $WebSiteUrl -OwnerAlias $SiteOwner -Name "Store" -Template $webTemplate            
            Write-Host "Root Site Collection created ..."

$site = Get-SPSite $WebSiteUrl
    $site.AllWebs[0].AnonymousState = [Microsoft.SharePoint.SPWeb+WebAnonymousState]::"On";

For subsites:

New-SPWeb –url http://vsharepoint/HyperV -name "All about HyperV" 
-template STS#0 –AddToTopNav –UniquePermissions –UseParentTopNav

For site template codes check: http://www.funwithsharepoint.com/sharepoint-2013-site-templates-codes-for-powershell/


  • Thanks Aanchal for quick response.I would like to know how to pass template name which is dynamic(Are you saaying it $webTemplates in example).I mean I will read it from a list.
    – user13408
    Jun 26 '14 at 16:17
  • Read from sharepoint list?
    – Aanchal
    Jun 26 '14 at 16:18
  • Yes I have created a list form where user will enter the template name and site name.I want to read those values in variables and pass it to example you gave.
    – user13408
    Jun 26 '14 at 16:21
  • You can read from that list and pass that : robertkuzma.com/2012/09/…
    – Aanchal
    Jun 26 '14 at 16:22
$url = "http://teams.contoso.com/depts/IT"
$ContentDatabase = "SharePoint_Content_Teams"

# OK… the content database is not something you are able to specify

# when creating a site collection in the user interface, but it’s so

# important and so many people wish it were there, that I’m adding it

# to this script

$WebsiteName = "Information Technology"
$WebsiteDesc = "Information Technology department team site"
$Template = "STS#0"
# If you don’t know the names of templates,

# enter the command GET-SPWebTemplate

$PrimaryLogin = "CONTOSO\LolaJ"
$PrimaryDisplay = "Jacobsen, Lola"
$PrimaryEmail = "Lola.Jacobsen@contoso.com"

$SecondaryLogin = "CONTOSO\AprilM"
$SecondaryDisplay = "Meyer, April"
$SecondaryEmail = "April.Meyer@contoso.com"

$MembersGroup = "$WebsiteName Members"
$ViewersGroup = "Viewers"

New-SPSite -Url $url –ContentDatabase $ContentDatabase -Name $WebsiteName –Description $WebsiteDesc  -Template $Template -OwnerAlias $PrimaryLogin –OwnerEmail $PrimaryEmail -SecondaryOwnerAlias $SecondaryLogin -SecondaryEmail $SecondaryEmail

$web = Get-SPWeb $url

$PrimaryAdmin = Get-SPUser $PrimaryLogin -Web $url
$PrimaryAdmin.Name = $PrimaryDisplay
$SecondaryAdmin = Get-SPUser $SecondaryLogin -Web $url
$SecondaryAdmin.Name = $SecondaryDisplay


Link: http://sharepointpromag.com/sharepoint/create-sharepoint-site-collection-windows-powershell-ui-style

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy