I just finished a tenant to tenant migration where I had to migrate: “Teams, Exchange, OneDrive & SharePoint”. Therefore, I added a service account to all teams. After that, I needed to bulk remove the Teams user from all my Teams.
Of course, I used Powershell to do so.
Prerequisites
You need 1 Powershell module to run the script, “MicrosoftTeams”.
To install these modules, use the following script:
$Modules = "MicrosoftTeams" $InstalledModules = Get-InstalledModule foreach ($Module in $Modules) { If ($InstalledModules.name -notcontains $Module) { Write-Host "Installing module $$Module" Install-Module $Module -Force } Else { Write-Host "$Module Module already installed" } }
After that, you are ready to start!
The script
The script is fairly simple. This is the syntaxis:
The script removes the user if it is a member of the team. If the user is not a member of the team, the script will skip the team.
Optionally, you can enable the “-Role Owner”. If the user is the only Owner of the team. He/She will be removed.
$User = 'User You want to Remove' $Teams = Get-Team foreach ($Team in $Teams) { $Members = Get-TeamUser -GroupId $Team.GroupId If ($Members.User -contains $User) { Write-Host "Removing $($User) from $($Team.Alias)" Remove-TeamUser -GroupId $Team.GroupId -User $user #Optional: -Role Owner } else { Write-Host "Skip Team:$($Team.Alias) since $($Owner) is not a member" } }
Usage:
Change the $user to the user you want to remove from the teams.
Connect to Microsoft Teams Powershell
And run the script.
Example
It is always nice to see exactly what you need to do to run a certain script. That is what I will show you.
Firstly, we install the module:
In my case, the module is already installed.
After that, we connect to Microsoft Teams Powershell by using:
Connect-MicrosoftTeams
Result:
After that, we run the script:
And that is how you bulk remove Teams User!
References
Microsoft Docs
Other posts:
Add Exclusion to Microsoft Defender
i want to remove an administator/owner of all the teams because this user has left our company.
I used the script (I changed the $user to the user i want to remove) but the administrator owner isn’t removed of the teams.
What is going wrong? Can you help me please with this script?
$User = ‘name of the user’
$Teams = Get-Team
foreach ($Team in $Teams) {
$Members = Get-TeamUser -GroupId $Team.GroupId
If ($Members.User -contains $Owner) {
Write-Host “Removing $($Owner) from $($Team.Alias)”
Remove-TeamUser -GroupId $Team.GroupId -User $user -Role Owner
}
else
{
Write-Host “Skip Team:$($Team.Alias) since $($Owner) is not a member”
}
}
Hi Laura,
Thanks for your comment. What does the script output? Do you get an error message? It did work at the time for me.
KR,
Niels
$Owner is not defined in the script. It should be $User not $Owner… Line 8, 10 and 16…
That is completely true. Thanks for your comment. I’ll change the script.
The Team.Alias also provided no output, so I changed to Team.Displayname which worked. (see below)
##REMOVING A USER FROM ALL TEAMS##
$User = “user@company.com”
$Teams = Get-Team
foreach ($Team in $Teams) {
$Members = Get-TeamUser -GroupId $Team.GroupId
If ($Members.User -contains $User) {
Write-Host “Removing $($User) from $($Team.DisplayName)”
Remove-TeamUser -GroupId $Team.GroupId -User $user #Optional: -Role Owner
}
else
{
Write-Host “Skip Team:$($Team.Alias) since $($User) is not a member”
}
}
###Adding a user to all Teams###
$User = “name@company.com”
$Teams = Get-Team
foreach ($Team in $Teams) {
$Members = Get-TeamUser -GroupId $Team.GroupId
If ($Members.User -notcontains $User) {
Write-Host “Adding $($User) to $($Team.DisplayName)”
Add-TeamUser -GroupId $Team.GroupId -User $user #Optional: -Role Member
}
else
{
Write-Host “Skip Team:$($Team.Alias) since $($User) is already a member”
}
}
Hi Rory,
Thanks for your input. I will update the blog accordingly.
Kind Regards,
Niels