SharePoint Online: Add User to Group using PowerShell
Requirement: SharePoint Online PowerShell to Add User to Group
How to add user to existing groups in SharePoint Online?
Each SharePoint Online site comes with a default set of SharePoint groups, such as Owners, Members and Visitors. You can add users to these groups, so that you can manage group permissions in a single place instead of managing each individual user. Creating SharePoint groups with custom permission levels also possible.
SharePoint Online PowerShell to Add User to Group
Here is how we can add user to group using PowerShell in SharePoint Online.
SharePoint Online: PowerShell to Add User to Owner Group
Here is how to add User to associated owner group of the site.
SharePoint Online: Add user to Group using PnP PowerShell
Here is the SharePoint Online PnP PowerShell to add user to group
Can I add a user to multiple Sites? Sure why not!
To add users from a CSV file to SharePoint Online groups, use: SharePoint Online: Bulk Add Users to Groups from a CSV File using PowerShell
How to add user to existing groups in SharePoint Online?
Each SharePoint Online site comes with a default set of SharePoint groups, such as Owners, Members and Visitors. You can add users to these groups, so that you can manage group permissions in a single place instead of managing each individual user. Creating SharePoint groups with custom permission levels also possible.
- Login to SharePoint online site >> Click on Site Settings >> Under Site Settings page, in the Users And Permissions section, click on "Site Permissions" link.
- On the Permissions page, click on "Grant Permissions" button in the ribbon from Permissions tab.
- In the Share dialog box, enter name / email addresses
- Click on "Hide Options" link button. In the Select A Group Or Permission Level list box, select the appropriate group name such as "Members"
- Click Share button.
SharePoint Online PowerShell to Add User to Group
Here is how we can add user to group using PowerShell in SharePoint Online.
#Load SharePoint CSOM Assemblies Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll" Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll" #Variables for Processing $SiteURL = "https://crescent.sharepoint.com/Sites/Sales" $UserAccount = "[email protected]" $GroupName="Sales Members" #Setup Credentials to connect $Cred = Get-Credential $Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password) Try { #Setup the context $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL) $Ctx.Credentials = $Cred #Get the Web and Group $Web = $Ctx.Web $Group= $Web.SiteGroups.GetByName($GroupName) #ensure user sharepoint online powershell - Resolve the User $User=$web.EnsureUser($UserAccount) #Add user to the group $Result = $Group.Users.AddUser($User) $Ctx.Load($Result) $Ctx.ExecuteQuery() write-host -f Green "User '$UserAccount' has been added to '$GroupName'" } Catch { write-host -f Red "Error Adding user to Group!" $_.Exception.Message }You can also use Add-SPOUser using SharePoint online management shell : SharePoint Online: PowerShell to Add Users to Group
SharePoint Online: PowerShell to Add User to Owner Group
Here is how to add User to associated owner group of the site.
#Load SharePoint CSOM Assemblies Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll" Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll" #Variables for Processing $SiteURL = "https://crescent.sharepoint.com/Sites/marketing" $UserAccount = "[email protected]" #Setup Credentials to connect $Cred = Get-Credential Try { #Setup the context $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL) $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password) #Get the Default Owner Group of the Web $Web = $Ctx.Web $Ctx.Load($Web) $Ctx.Load($Web.AssociatedOwnerGroup) $Ctx.ExecuteQuery() #ensure the sharepoint online user $User=$web.EnsureUser($UserAccount) #sharepoint online powershell add user to owner group $Web.AssociatedOwnerGroup.Users.AddUser($User) | Out-null $Ctx.ExecuteQuery() write-host -f Green "User '$UserAccount' has been added to Owners Group of the Site!" } Catch { write-host -f Red "Error:" $_.Exception.Message }
SharePoint Online: Add user to Group using PnP PowerShell
Here is the SharePoint Online PnP PowerShell to add user to group
#Config Variables $SiteURL = "https://crescenttech.sharepoint.com/Sales" $GroupName="Sales Portal Members" $UserLoginID = "[email protected]" #Connect to PNP Online Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential) #sharepoint online powershell to add user to group Add-PnPUserToGroup -LoginName $UserLoginID -Identity $GroupNameSimilarly, if you want to add user to default members group of the site, use:
#Config Variables $SiteURL = "https://crescent.sharepoint.com/sites/marketing" $UserAccount= "[email protected]" #Connect to PNP Online Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential) #Get the Default Members Group $MembersGroup = Get-PnPGroup -AssociatedMemberGroup #sharepoint online pnp powershell add user to group Add-PnPUserToGroup -LoginName $UserAccount -Identity $MembersGroup
Can I add a user to multiple Sites? Sure why not!
#Define Sites List $Sites = "https://crescent.sharepoint.com/sites/Sales", "https://crescent.sharepoint.com/sites/Marketing", "https://crescent.sharepoint.com/sites/Purchase", "https://crescent.sharepoint.com/sites/Operations" #Loop Through Each Site ForEach ($URL in $Sites) { #Connect to Site Connect-PnPOnline -Url $URL -UseWebLogin #Get the Default Members Group $MembersGroup = Get-PnPGroup -AssociatedMemberGroup #Add User to Group Add-PnPUserToGroup -Identity $MembersGroup -EmailAddress "[email protected]" Write-host "Added User to Site:"$URL }
To add users from a CSV file to SharePoint Online groups, use: SharePoint Online: Bulk Add Users to Groups from a CSV File using PowerShell
Thanks so much for your help. This save me alot of time.
ReplyDelete