Remove user from Office 365 Group using PowerShell

We can use the Exchange Online powershell cmdlet Remove-UnifiedGroupLinks to remove members, owners and subscribers from Office 365 groups. The Remove-UnifiedGroupLinks cmdlet includes the following parameters:

Identity – This parameter specifies the Office 365 Group that you want to update. You can use alias, display name, or email address of the unified group that you
want to modify.
Links  – This parameter specifies the recipients to remove from the Office 365 Group.You can use alias, display name, or email address of the user that you want to remove.
LinkType – Members, Owners, or Subscribers.

Before proceed, run the following commands to connect Exchange Online Powershell session.

$365Logon = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $365Logon -Authentication Basic -AllowRedirection
Import-PSSession $Session

Remove user from Office 365 Group

The following command remove the member [email protected] from the Office 365 Group named TestO365Group. We need to set the parameter LinkType as Members to remove user from being a member.

Remove-UnifiedGroupLinks –Identity "TestO365Group" –Links [email protected] –LinkType Members -Confirm:$false

Remove multiple users from Office 365 Group

In above command the parameter Links accept multiple values, so we can easily remove multiple members by running single command by passing user identities as comma separated values (Ex: value1,value2….). If the user ids contain spaces we need to enclose every value by quotation marks (Ex: “value1″,”value2”,..).

The below example removes the members [email protected] and [email protected] from the Office 365 Group named TestO365Group.

Remove-UnifiedGroupLinks –Identity "TestO365Group" –Links [email protected] ,[email protected] –LinkType Members -Confirm:$false

Remove bulk users from Office 365 Group (from CSV file)

You can use the below powershell commands to remove bulk members from an office 365 group by importing users from csv file. Consider the csv file members.csv that includes the column member which holds the member identity in each row of the csv file.

Import-CSV "C:\members.csv" | ForEach-Object {
Remove-UnifiedGroupLinks –Identity "TestO365Group" –Links $_.member –LinkType Members -Confirm:$false
Write-Host "The user" $_.member "removed"
}

Find and list members of Office 365 Group

Once we added or removed users in unified group, we can use the Get-UnifiedGroupLinks cmdlet to get members of a specific group. The below command lists all the members of the given group.

Get-UnifiedGroupLinks –Identity "TestO365Group" –LinkType Members -ResultSize "Unlimited"

You can also export all the member details to csv file using below command.

Get-UnifiedGroupLinks –Identity "TestO365Group" –LinkType Members -ResultSize "Unlimited" | 
Select DisplayName,Name,PrimarySMTPAddress |
Export-CSV "C:\Office365GroupMembers.csv" -NoTypeInformation -Encoding UTF8
Advertisement

Leave a Comment