Create default site groups on site collection

26/11/2013 12:37

#Creates site collections

 

 

function CreateTeamSiteCollection

{

param($siteUrl, $title, $admins)

"=================================="

$script:MyInvocation.MyCommand.Name

"=================================="

if(-not $title)

{

$title = $siteUrl

}

$site = Get-SPSite -Identity $siteUrl -ErrorAction SilentlyContinue

if($site -ne $null)

{

"Site '{0}' already exist." -f $siteUrl

}

else

{

$template = Get-SPWebTemplate "STS#0"

$currentUser = $env:UserDomain + '\' + $env:USERNAME

"Creating Team Site '{0}' ..." -f $siteUrl

#orig $void = New-SPSite -Url $siteUrl -OwnerAlias $currentUser -Template $template -Name $title

$site = New-SPSite -Url $siteUrl -OwnerAlias $currentUser -Template $template -Name $title

"Team Site '{0}' created." -f $siteUrl

 

foreach($admin in $admins)

{

$userName = $env:UserDomain + "\" + $admin

 

"Adding '{0}' to site collection administrators" -f $userName

$user = $site.RootWeb.EnsureUSer($userName)

if($user)

{

$user.IsSiteAdmin = $true

$user.Update()

 

}

 

}

$web = $site.RootWeb

$web.CreateDefaultAssociatedGroups($currentUser, "", $site.Title)

$web.Update();

$web.Dispose()

$site.Dispose()

}

}

$admins = @("jan.tomas", "marian.trubiansky")

CreateTeamSiteCollection -siteUrl "https://externalsp2013/sites/grouptest" -title "Group Test" -admins $admins