Create a SharePoint Group using PowerShell

Requirement: Create a new SharePoint Group using PowerShell

create a SharePoint group powershell

SharePoint groups simplify permission management by assigning a set of users to a group and assigning permission levels to them through the group. There are default groups that get created based on the site template you choose while creating site collections, Such as Owners, Members, and Visitors. In this post, we’ll show you how to use PowerShell to create a SharePoint group.

PowerShell to Create SharePoint Security Group

Group creation is a common task for SharePoint administrators, and Here is my script to create a 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 "https://sales.crescent.com" "Sales Managers" "Edit" "Group for Sales Managers" 

This script creates a SharePoint user group using PowerShell. You can also create SharePoint user groups from web UI: How to Add a New User Group in SharePoint?

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

One thought on “Create a SharePoint Group using PowerShell

  • How can we add custom webpart to NewForm.aspx page (default) using “power shell script in sharepoint 2013”.

    Ex: I have MyCustomList and add new item,i will get NewForm.aspx in that i need to add my custom(visual webpart)to the page using powershell script.

    Kindly let us know any one developed or any suggestions

    Thanks

    Guntaka

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *