Home > Software design >  Powershell script to delete Microsoft Team
Powershell script to delete Microsoft Team

Time:12-06

I am working on a script to remove a Microsoft team in a tenant using remove-team. How would I go about putting the output of get-team into menu options for the user to choose which team to delete?

Would I need to put all of the teams in an array with just selecting the ObjectID which remove-team needS? i have started off with the below. i can enter $teams[0,1] and it does show the first two teams.

i would like the user to have menu options like below.

  1. Team1
  2. Team2
  3. Team3

Please enter the number of the team you would like to delete


$teams = Get-Team
foreach ( $team in $teams) { 
  
}

CodePudding user response:

You can create a for loop to iterate through the collection of Teams assigning a number to their corresponding index number:

$Teams = Get-Team

for ($i = 0; $i -lt $Teams.Count; $i  )
{
    "{0}: {1}" -f $i, $Teams[$i].DisplayName
}

# multiple can be selected if comman seperated
$Selection = (Read-Host -Prompt "Select Team(s) to remove").Split(',').Trim()

foreach ($number in $Selection) 
{
    $Teams[$number]
    #Remove-Team -GroupId $Teams[$number].GroupID 
}

<# Output
0: TeamsOne
1: TeamsTwo
#>

All that's left to do is prompt for the number of teams to remove and re-select it using the index number.

  • Used the -f string format operator as it should allow you to assign the current iteration of $i to the choice number, and the Display Name of the teams; giving it a feel of choice selection.
  • Using the .Split() method let's you make a multiple choice selection given that it's separated by a comma.
  • Finally, use a foreach loop to iterate through the numbers input into our Read-Host assigned to $selection. Which gives you the same index number that will be used to select our choice from $Teams.

Disclaimer: I don't have teams installed on my machine so I went based off using some pictures from google results, and the MSDocs on the cmdlets.

  • Related