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.
  • 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.
    SharePoint Online PowerShell to Add User to Group
  • 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"
    sharepoint online add user to group powershell
  • Click Share button.
Alternatively, You can Go to Site Settings page >> Click on "People and Groups" link under Users and Permissions section. On the People and Groups page, Click on the name of the group from the Groups list in left navigation. On the Groups's page, Click on the arrow next to the "New" link and Add Users.

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 = "Salaudeen@crescent.com"
$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)

    #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: Add user to Group using PnP PowerShell 
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com/Sales"
$GroupName="Sales Portal Members"
$UserLoginID = "Salaudeen@CrescentTech.com"

#Connect to PNP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#sharepoint online powershell to add user to group
Add-PnPUserToGroup -LoginName $UserLoginID -Identity $GroupName 
SharePoint Online: Add User to Group using PowerShell SharePoint Online: Add User to Group using PowerShell Reviewed by Salaudeen Rajack on April 29, 2016 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.