SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am creating sites through powershell and creating the groups for them. I have a situation where if the group is already existing group then I have to add that group to the site. Coz might be possible the site is not present and the group is present. For ex: the site name is "xyz" and the group name is "xyz_admin". Here if the group is already existing group while creating the site then I have to add it to the site.

I am adding the group like this:

Add-SPGroup.ps1 -url $url -Group ($webname+"_admins") -Role "Fuld kontrol" -Owner $global:loginName

but it gives me an error when the group is present.

Any help is appreciated in this aspect.

Thanks in advance.

share|improve this question
up vote 0 down vote accepted
if ($SPWeb.SiteGroups[$webname+"_admins"] -ne $null){
  Write-Host "Group "$GroupName" already exists!"
  $desiredGroup = $SPWeb.SiteGroups[$webname+"_admins"]
  $roleAssignment = New-Object Microsoft.SharePoint.SPRoleAssignment($desiredGroup)
  $role = $SPWeb.RoleDefinitions["Contribute"]
 } else
  $owner = $SPWeb | Get-SPUser $OwnerName
  if ($MemberName -ne "") {  $member = $SPWeb | Get-SPUser $MemberName }
  $SPWeb.SiteGroups.Add($GroupName, $owner, $member, $Description)
  $SPGroup = $SPWeb.SiteGroups[$GroupName]

Powershell to create Sharepoint groups
Adding a security group to a SharePoint site via PowerShell

share|improve this answer
HI @Arsalan here you are checking the group and just breaking the condition. But I want to add that group to the site which is newly created. for instance if ($SPWeb.SiteGroups[$GroupName] -ne $null){ Write-Host "Group "$GroupName" already exists!" **Do something here to add this group to the site.** } – Ankit Dec 3 '12 at 8:19
Updated the answer! – Arsalan Adam Khatri Dec 3 '12 at 8:29
Thanks.. :) this worked for me.. – Ankit Dec 3 '12 at 9:21

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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