Hello, how do I bulk remove members sorted by Teams names?

Viewed 42

I have the following code but unfortunately I am stuck with the -user parameter for remove-azureaduser, it comes back as resource does not exist, any powershell expert willing to help?

Edit: The idea here was that it would work with multiple teams names as well

Connect-MicrosoftTeams

$Teams = Get-Team -DisplayName "test"

$users = Get-TeamUser -GroupId $Teams.GroupId -Role Member | Select-Object user

Foreach ($user in $users) {

Remove-TeamUser -GroupId $Teams.GroupId -User $user

}
1 Answers

Docu: https://docs.microsoft.com/en-us/powershell/module/teams/remove-teamuser?view=teams-ps

As I dont have this cmdlets I can't really verify but I think this is wrong:

$users = Get-TeamUser -GroupId $Teams.GroupId -Role Member | Select-Object user

Select-Object user? Think this does not work, based on the documentation u need to provide the userPrincipalName to the cmdlet Remove-TeamUser and I don't think the object returned by the Get-teamUser cmdlet has a property called user.

Simply do this:

$teams #array containing n teams
foreach ($team in $teams){
    $users = Get-TeamUser -GroupId $Team.GroupId -Role Member
    Foreach ($user in $users) {
        Remove-TeamUser -GroupId $Teams.GroupId -User $user.userPrincipalName
    }
}

I don't know if the attribute is called userPrincipalName or UPN... just output one user ($user[0]) to see the list of available attributes.

Related