How to Add a User to a SharePoint Group using PowerShell?

Requirement: Add User to SharePoint Group using PowerShell.

How to add a user to a group in SharePoint?

Adding users to groups is a common task that administrators need to perform to organize and manage permissions in SharePoint environments. In this blog post, we will walk you through the process of adding a user to a SharePoint group. We will also show you how to add a user to a SharePoint group using PowerShell.

Follow the below steps to add a user to a particular group on the SharePoint site:

  1. Go to your SharePoint site, click the Site Settings gear icon, and select “Site settings.”
  2. Click on the “People and Groups” link under “Users and Permissions”.
  3. Click on the SharePoint user group to which you want to add users, such as “Marketing Members”.
  4. Click on the “New” button >> and select “Add Users”.
    sharepoint group add user powershell
  5. Enter the names of the users to add. Make sure all your entries are resolved.
  6. Click on the “Share” button to complete adding a user to the SharePoint group.
    sharepoint powershell add user to group

The users you have entered are now part of the SharePoint group! By default, new users receive an email notification, and we can customize the message text if needed. We can also choose not to send an email by clicking Show Options and clearing the “Send an email invitation” checkbox.

Add User to SharePoint group with PowerShell

SharePoint provides two cmdlets to add a user to a group using PowerShell.

New-SPUser

The New-SPUser cmdlet adds an existing active directory (or whatever authentication provider) to SharePoint and adds it to the appropriate group specified.

New-SPUser -UserAlias "domain\user" -Web "https://sharepoint.crescent.com/sites/marketing" -Group "Marketing Owners"

This will add a new user to the SharePoint site to the particular group. If you execute this command the next time (without deleting the user from the site collection), it won’t have any effect!

Set-SPUser

The Set-SPUser cmdlet adds an existing SharePoint user to an existing group on the given site.

Set-SPUser -Identity "domain\user" -Web "https://sharepoint.crescent.com/sites/marketing" -Group "Marketing Owners"

This will add the existing SharePoint user’s account to the provided group but will give an error when you try to add a new user to the SharePoint site. (which is obvious! We can’t set the user property, if the user doesn’t exist on the SharePoint site, isn’t it?)

The Set-SPUser cmdlet can add direct site permissions as well. Here is an example:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Parameters
$WebURL = "https://intranet.crescent.com"
$UserID="Crescent\Charles"
$PermissionLevel="Read"

#Get the Web 
$Web = Get-SPWeb $WebURL
$User = $web.EnsureUser($UserID)

#Grant Permission to User
Set-SPUser -Identity $User -Web $WebURL -AddPermissionLevel "Read"

Add User to SharePoint Group using PowerShell Script

In addition to the New-SPUser and Set-SPUser cmdlets, let’s use the native object model APIs to add a user to a group in SharePoint using PowerShell.

#Get the Web
$web=Get-SPWeb "https://sharepoint.crescent.com/sites/marketing"
#Get the SharePoint Group
$Group= $web.Groups["Marketing Owners"]
$userName = "domain\userName"

#Add User to the site collection
$user = $web.EnsureUser($UserName)

#Add User to the Group
$Group.AddUser($user) 
web.group vs web.sitegroup – SPWeb.Group refers to the groups which are used in the specified SPWeb object (If SPWeb uses unique permissions). Whereas SPWeb.SiteGroups refers to a collection of all groups in the site collection.

If you want to add users to SharePoint group in SharePoint Online, refer to SharePoint Online: How to Add Users to SharePoint Group using PowerShell.

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

5 thoughts on “How to Add a User to a SharePoint Group using PowerShell?

Leave a Reply

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