Quantcast
Channel: SharePoint Diary
Viewing all articles
Browse latest Browse all 1058

Create a SharePoint Group using PowerShell

$
0
0
Here is my script to create new SharePoint group using PowerShell:
Add-PSSnapin Microsoft.SharePoint.PowerShell –ErrorAction SilentlyContinue

#Custom Function to Create new SharePoint Group
function Create-SPGroup
{
param ($SiteURL, $GroupName, $PermissionLevel, $GroupDescription)

try
{
#Get the Web
$web = Get-SPWeb -Identity $SiteURL

if($web -ne $null)
{
#Check if Group Exists already
if ($web.SiteGroups[$GroupName] -ne $null)
{
write-Host "Group $GroupName exists Already!" -ForegroundColor Red
}
else
{
#Create SharePoint Group
$Web.SiteGroups.Add($GroupName, $web.Site.Owner, $web.Site.Owner, $GroupDescription)
#Get the newly created group and assign permission to it
$Group = $web.SiteGroups[$groupName]
$roleAssignment = new-object Microsoft.SharePoint.SPRoleAssignment($group)
$roleDefinition = $web.Site.RootWeb.RoleDefinitions[$permissionLevel]
$roleAssignment.RoleDefinitionBindings.Add($roleDefinition)
$web.RoleAssignments.Add($roleAssignment)
$web.Update()

write-Host "Group: $GroupName created successfully!" -ForegroundColor Green
}

$web.Dispose()
}
}
catch [System.Exception]
{
write-host $_.Exception.ToString() -ForegroundColor Red
}
}

#Call the function to create Sharepoint group
Create-SPGroup "http://sales.crescent.com" "Sales Managers" "Edit" "Group for Sales Managers"

Viewing all articles
Browse latest Browse all 1058

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>