Sunday, April 17, 2016

How to Add User to SharePoint Group using PowerShell

Requirement: Add User to SharePoint group using PowerShell

Solution:
From SharePoint site, you can follow below steps to add an user to particular group.

  • Go to your SharePoint site and click on Site Settings gear icon >> Select "Site settings"
  • Click on "People and groups" link under "Users and Permissions"
  • Click on the SharePoint user group to which you want to add users, such as "Marketing Members"
  • Click on "New" button >> and select "Add Users".
    sharepoint group add user powershell
  • Enter the names of the users to add. Make sure all your entries are resolved.
  • Click on "Share" button to to complete add user to SharePoint group.
    sharepoint powershell add user to group
The users have entered are not added to the group!

Add User to SharePoint group with PowerShell
There are two cmdlets provided by SharePoint to add user to group using PowerShell.

New-SPUser New-SPUser cmdlet adds an existing active directory (or whatever authentication provider) to SharePoint to the appropriate group specified.
New-SPUser -UserAlias "domain\user" -Web "http://sharepoint.crescent.com/sites/marketing" -Group "Marketing Owners"
This will create a add a new User to SharePoint site to the particular group. If you execute this command for the next time, (without deleting the user from site collection) this command has no effect!

Set-SPUser:
Set-SPUser cmdlet adds an existing SharePoint user to an existing group in the given site.
Set-SPUser -Identity "domain\user" -Web "http://sharepoint.crescent.com/sites/marketing" -Group "Marketing Owners"
This will add existing SharePoint users account to the provided group, but will give error when you try add a new user to SharePoint site. (which is obvious! We can't set the user property, if the user doesn't exists in SharePoint site, isn't it?)

Add User to SharePoint Group using PowerShell Script
Other than New-SPUser and Set-SPUser cmdlets, lets use the native object model APIs to add user to group in SharePoint using PowerShell.
#Get the Web
$web=Get-SPWeb "http://sharepoint.crescent.com/sites/marketing"
#Get the 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) 



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...