How to remove yourself from any team of Microsoft Teams

Posted on 21 September 2021

TL;DR Do you use Microsoft Teams in your organization and cannot remove yourself from a team? Read this!

**If you are using Microsoft Teams in your organization, as we are doing at University of Calabria, these few words may save your days! Possibly you already noticed that you cannot leave a team of your organization unless you are an owner (and not the last one) of the team. The only way to leave a team seems to ask an owner to kick us out, which can overload owners of big teams like classes of 100-200 students. Fortunately, such a restriction is applied only on the user interface, and not on the underlying Microsoft servers.

In short, an easy way to remove yourself from a team is via the PowerShell. I prepared a simple script that should work under several circumstances.

$ cat >remove-me-from-this-team.ps1 <<'EOF'
#!/usr/bin/pwsh

param(
    [string] $TeamName
)

if($TeamName -eq "__win-superpower__") {
    Set-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseS
trongCrypto' -Value '1' -Type DWord
    Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto'
 -Value '1' -Type DWord
    Set-ExecutionPolicy -ExecutionPolicy Unrestricted
    Exit
}

if($TeamName -eq "__install-modules__") {
    Install-Module -Name PowerShellGet -Force -AllowClobber
    Install-Module -Name MicrosoftTeams -Force -AllowClobber
    Exit
}

$account = Connect-MicrosoftTeams | Select -ExpandProperty "Account"
$group_id = Get-Team -DisplayName "$TeamName" | Select -ExpandProperty "GroupId"
Remove-TeamUser -GroupId $group_id -User $account
EOF
$ chmod +x remove-me-from-this-team.ps1

You need two modules to run the script, namely PowerShellGet and MicrosoftTeams, but you can install them via the script (read until the end). After everything is set up, just pass to the script one parameter, the name of the team you want to be removed:

$ ./remove-me-from-this-team.ps1 "Secure Software Design"

Your browser will ask to authenticate your Microsoft Teams user, and then you will be removed from the team.

In order to install the required modules, run the script as follows:

$ ./remove-me-from-this-team.ps1 __install-modules__

You need to run the above command only once. If you are using an old Windows, you need to tell the PowerShell to use non-deprecated cryptographic protocols with the following command:

$ ./remove-me-from-this-team.ps1 __win-superpower__

Again, you need to run the above command only once (and only on old Windows machines). Also note that on Windows you may want to run the script as follows:

C:> pwsh remove-me-from-this-team.ps1 __win-superpower__
C:> pwsh remove-me-from-this-team.ps1 __install-modules__
C:> pwsh remove-me-from-this-team.ps1 "Secure Software Design"

I hope that this may help you! The script can be downloaded here:

remove-me-from-this-team.ps1