Sharepoint Online – Add members and owners via PowerShell

To add members and owners to a SharePoint Online TeamSite you have the below commands avaiable:

Note that if you need to make someone Owner of the Teamsite, the user first needs to be member! And then you make the user Owner.

#Get Groupnames
Get-UnifiedGroup

#Check for current members
Get-UnifiedGroupLinks –Identity "groupname" –LinkType Members

#Check for current owners
Get-UnifiedGroupLinks –Identity "groupname" –LinkType Owners

#Add user as member
Add-UnifiedGroupLinks –Identity "groupname" –LinkType Members –Links info@iterrors.com

#Add user as owner (first make the user ‘member’)!!
Add-UnifiedGroupLinks –Identity "groupname" –LinkType Owner –Links info@iterrors.com

#Remove user as member
Remove-UnifiedGroupLinks –Identity "groupname" –LinkType Members –Links info@iterrors.com -Confirm:$false

#Remove user as Owner (Don’t forget to also remove the Members entry).
Remove-UnifiedGroupLinks –Identity "groupname" –LinkType Owners –Links info@iterrors.com -Confirm:$false

#Extra option, turn off the Welcome Message.
Set-UnifiedGroup "groupname" -UnifiedGroupWelcomeMessageEnabled:$false

As a followup on this topic i’ve created a sample script you can use;

#Connect to Exchange Online
Connect-ExchangeOnline

#Export all unifiedgroups to csv
Get-UnifiedGroup | select DisplayName, Identity, PrimarySmtpAddress | Export-CSV -Path C:\Temp\unifiedgroups.csv

#Load CSV and define user
$CSVPath = "c:\temp\unifiedgroups.csv"
$CSV = Import-Csv -Path $CSVPath
$user = "info[at]iterrors.com"

#Script
foreach ($CSVline in $CSV) {
$group = $CSVline.DisplayName

#optional, turn off the “You’ve been added to this group email”
Set-UnifiedGroup "$group" -UnifiedGroupWelcomeMessageEnabled:$false

#adding user to group as owner
Write-Host "Adding $user to $group" -ForegroundColor Cyan

#first add as member before making the user owner
Add-UnifiedGroupLinks –Identity "$group" –LinkType Members –Links $user
#secondly, add the user as owner
Add-UnifiedGroupLinks –Identity "$group" –LinkType Owners –Links $user

#optional, if you’ve turned off the mail aboven, turn on the “You’ve been added to this group email”
Set-UnifiedGroup "$group" -UnifiedGroupWelcomeMessageEnabled:$true

}

I expierenced the following errors;
The operation couldn’t be performed because object ” couldn’t be found on ”
It could be that groups have a ‘special character’ in their name, better to use ‘Idenity’ then instead of ‘DisplayName’.
The operation couldn’t be performed because ” matches multiple entries
It could be that groups have a duplicate name, better to use ‘Idenity’ then instead of ‘DisplayName’.