The estimated reading time 2 minutes

Hi everyone,

today I asked myself, how can you find microsoft teams with guest users integrated. I  did a script doing exactly this challenge. It collects all teams with guests integrated (only if you are a member) and displays the results in seperated windows. Also there will be an HTML report next to the script.

NOTE: this script is user-centric so you will not see teams with guests where logon user is no member, have look at my new script user-idependent LINK.

here my first script

#connect ms teams 
#generate array
$teams = @()
$externalteams = @()
#get all teams in organization
$teams = get-team
#go through every team and search for external users
foreach ($team in $teams){
  #get groupid of the team 
  $groupid = ($team.groupid)
  #search external users 
  $users = (Get-TeamUser -GroupId $team.groupid | Where-Object {$_.Role -eq "Guest"})
  #count external users
  $extcount = ($users.count)
  #go through every team an put teamname and teammembers in custom object
  foreach ($extuser in $users){
    #group id 
    $id = $team.groupid
    #get displayname from team
    $teamext = ((Get-Team | Where-Object {$_.groupid -eq "$id"}).DisplayName).ToString()
    #get the external users 
    $ext = $extuser.User
    #create custom object
    $externalteams += [pscustomobject]@{
      ExtUser   = $ext
      GroupID   = $id
      TeamName  = $teamext
 #check if there are some teams with external users or not 
  if ($extcount -eq "0"){
    Write-host "there are no external user added to any team in your organization" -ForegroundColor yellow
    #show custom object in powershell
    $externalteams | Out-GridView -Title "external members in Teams"
 $Header = @"
 TABLE {border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse;}
 TD {border-width: 1px; padding: 3px; border-style: solid; border-color: black;}
 $externalteams | ConvertTo-Html -Property ExtUser,TeamName,GroupID -Head $Header | Out-File -FilePath .\msteam-guests.html
 Invoke-Expression ".\msteam-guests.html"    

The heart of this script is “Get-TeamUser -GroupId $team.groupid | Where-Object {$_.Role -eq „Guest“}”.

One requirement for the script is to install powershell cmdlets for microsoft teams. Also you have to be connected to microsoft teams.

Have a look at my article.

As mentioned there will also be an html report next to the script.

The script can also run directly in powershell. ISE is not required. Another requirement is powershell in version 3.0.

Looking forward to comments.

If you like this article please push “this was helpful”.

Have fun.

Print Friendly, PDF & Email
  • Was this Helpful ?
  • yes   no