SharePoint: Create Group using PowerShell

Power Shell Script:
cls
Remove-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
Add-PSSnapin Microsoft.SharePoint.Powershell


$groupsXML = [xml] (Get-Content ("Group.XML"))
$Site= Get-SPSite -Identity $groupsXML.GRoups.Site;
$GroupName=$groupsXML.GRoups.GroupName;
$OwnerName=$groupsXML.GRoups.OwnerName;
$MemberName=$groupsXML.GRoups.MemberName;
$Description=$groupsXML.GRoups.Description;


 $SPWeb = $Site.RootWeb;
 if ($SPWeb.SiteGroups[$GroupName] -ne $null){
  Write-Host "Group "$GroupName" already exists!"
  Break;
 } 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]
  $SPWeb.RoleAssignments.Add($SPGroup)
 }


$SPWeb.Dispose()
return $ErrMessage


XML File:

<Groups>
<Site>Enter Your Site Url</Site>
<GroupName>Enter Your Group Name</GroupName>
<OwnerName>Enter Group Owner Name</OwnerName>
<memberName>Enter Group Member Name</memberName>
<Description>Add Description</Description>
</Groups>



Comments

Popular posts from this blog

C# Copy files from one server to another

Export SharePoint Online Group Users to Excel using Powershell