DEV Community

Cover image for Delete multiple Microsoft Teams using the script
Rajesh
Rajesh

Posted on

2 1

Delete multiple Microsoft Teams using the script

This is a cross-post from rjesh.com

Lately, I have been working on creating a Teams template using Graph APIs and PnP Tenant template. Quickly, I accumulated a lot of Teams and decided to do a clean-up. I have achieved it by combining Office 365 CLI with PowerShell Core. My goal is to delete only the specific teams and not everything in my tenant, and for all my test teams I have used a naming convention which starts with 'RJ'.

Install PowerShell Core

Install Office 365 CLI

## Get teams to delete
$teams = o365 teams team list -o json `
--query '[?starts_with(displayName, `RJ`) == `true`].id' `
| ConvertFrom-Json

foreach ($team  in  $teams) {
    Write-Host  "Deleting..."  $team -ForegroundColor Green
    o365 teams team remove -i $team --confirm
}

Above is a very simple script (✌🏼yay it works in Mac, PC and even in Cloud Shell ), the key thing to notice here is @office365cli support for --query parameter which uses JMESPath. In the query parameter, I have used starts_with function to get only my test Teams which starts with the display name 'RJ' and used team remove command to get rid of them.

You can change the query parameter as required to get the filtered result. For example, if you want to get all Teams except "Mark 8 Project Team"

 o365 teams team list -o json --query '[?displayName != `Mark8 Project Team`]'

Note Just like me If you are wondering, even after deleting the Teams it still shows up in the Teams App then you should read this - techcommunity post which details about the latency of few hours required.

If you want to delete the Office 365 groups then use the below script

## Get groups to delete
$groups = o365 aad o365group list -o json `
--query '[?starts_with(displayName, `SET`) == `true`].id' `
| ConvertFrom-Json

foreach ($group  in  $groups) {
    Write-Host  "Deleting..."  $group -ForegroundColor Green
    o365 aad o365group remove -i $group --confirm
}

Photo by Philipp Katzenberger on Unsplash.

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay