SharePoint Online: PowerShell to Add User to Group

Requirement: Add user to group in SharePoint online using PowerShell.
Prerequisites: You need to have SharePoint Online Management Shell installed in your client machine!

SharePoint Online PowerShell to add user to group
To add an individual user to SharePoint online site collection group using PowerShell, use Add-SPOUser cmdlet from SharePoint online management shell. Here is an example:

Step 1: Enter the connection Credentials for SharePoint Online Admin Center
$Credential = Get-credential
This prompts for user name and password. Key-in your SharePoint online Administrator credentials.

Step 2: Connect to SharePoint Online Service:
Connect-SPOService -url https://Crescent-admin.sharepoint.com -Credential $Credential

Step 3: Add User to SharePoint Online Site Group:
Once the connection is established successfully in the previous step, you can add user to group.
Add-SPOUser -Site "https://Crescent.sharepoint.com/sites/Sales" -Group "Sales Members" -LoginName salaudeen@Crescent.com
The SharePoint Online Management Shell looks like this:
add user to group in sharepoint online using powershell
This adds the given user to the group in SharePoint Online.
 
Bulk Add Users from a CSV File to SharePoint Online Group using PowerShell:
Now the next question is, What if you want to add 100's of users to a site collection? Here is how to add multiple users to groups in SharePoint online from a CSV file.

Step 1: Create a CSV file and fill in according to your requirements
sharepoint online powershell add user to group

Step 2: Use this PowerShell script to Read from CSV and import users to SharePoint Group:
#Connection paramaters
$AdminURL = "https://Crescent-admin.sharepoint.com/"
$AdminName = "SPAdmin@Crescent.com"
$CSVFile="C:\UsersToAdd.csv"

#User Names Password to connect 
$Password = Read-host -assecurestring "Enter Password for $AdminName" 
$Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminName, $Password

#Connect to SharePoint Online
Connect-SPOService -url $AdminURL -credential $Credential

#add multiple users to sharepoint online group powershell - Import Users from CSV
Import-Csv $CSVFile | ForEach-Object { Add-SPOUser -Site $_.SiteURL -Group $_.GroupName -LoginName $_.UserAccount}
You can also use CSOM PowerShell to add user to group. Here is how: SharePoint Online: Add user to group using PowerShell CSOM

PnP PowerShell to Bulk Add Multiple Users to Multiple Site's Group in SharePoint Online:
Here is an another case: How to add multiple users to multiple site collection's Owners group?
#Variables
$SiteURLs = "Sales","Intranet","Marketing" 
$UserIDs = "Salaudeen@crescent.com","Steve@crescent.com"

#Iterate through each site
ForEach($Site in $SiteURLs)
{
    #Connect to SharePoint Online Site
    Write-host "Connecting to Site: "$Site
    Connect-PnPOnline -Url https://actisportal.sharepoint.com/sites/$Site -UseWebLogin

    #Get the Associcated Owners group of the site
    $Web = Get-PnPWeb
    $Group = Get-PnPGroup -AssociatedOwnerGroup

    #Add Each user to the Group
    ForEach($User in $UserIDs)
    {
        Add-PnPUserToGroup -LoginName $User -Identity $Group
        Write-host -f Green "`tAdded $User to $($Group.Title)"
    }
}
SharePoint Online: PowerShell to Add User to Group SharePoint Online: PowerShell to Add User to Group Reviewed by Salaudeen Rajack on June 05, 2016 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.