skip to Main Content

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

}

2

Answers


  1. Chosen as BEST ANSWER

    After a small adjustmend, the answer is, thanks to Tony:

    $Teams = Get-Team -DisplayName "test"
    
    $users = Get-TeamUser -GroupId $Teams.GroupId -Role Member
    
    foreach ($team in $teams){
    
        $users = Get-TeamUser -GroupId $Team.GroupId -Role Member
    
        Foreach ($user in $users) {
    
            Remove-TeamUser -GroupId $Team.GroupId -User $user.User
    
        }
    
    }
    

  2. Docu: https://learn.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 $Team.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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search