Find Teams with guest users included via powershell user centric

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 
Connect-MicrosoftTeams
#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
    
    }
  else{
    #show custom object in powershell
    $externalteams | Out-GridView -Title "external members in Teams"
 $Header = @"
 <style>
 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;}
 </style>
"@
 $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 article helpful?
YesNo
0 0 votes
Article Rating
Subscribe
Notify of
guest
8 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
trackback
2 years ago

[…] Find Teams with guest users included via powershell user … […]

trackback
2 years ago

[…] Find Teams with guest users included via powershell user … […]

Akr
Akr
2 years ago

Hi there, splendid job.
I hope you do not mind me pointing something out, certain parts of your code increases execution time especially if you have a large set with teams.
for example:

►  $teamext = ((Get-Team | Where-Object {$_.groupid -eq "$id"}).DisplayName).ToString()

The $teamext part is running a cmdlet for each external user residing in a team, if you have 1000 teams with an average og 10 external users in every team, this is executed 10,000 times which is very time consuming the larger the team/external user combination gets.

The displayname can be retrieved from the already collected data set $team.DisplayName.

The way I perform this is by executing the following re-stacked version of your code:

foreach ($team in $teams)
{
 $users = (Get-TeamUser -GroupId $team.groupid | Where-Object {$_.Role -eq “Guest”})
 $extcount = ($users.count)
 foreach ($extuser in $users)
 {
 $arrayEntry = [pscustomobject]@{
     ExtUser  = $extuser.user
     GroupID  = $team.groupID
     TeamName = $team.DisplayName
    }
   $externalteams += $arrayEntry
 }     

}

Last edited 2 years ago by Akr
trackback
3 years ago

[…] may have found my old article from june 1st 2018 LINK. Some time has passed since this article was released. Yes I know there is a overview in Teams […]

trackback
3 years ago

[…] Some may have read my previous article about finding guests in teams, see link. […]

trackback
4 years ago

[…] some other blogs and coming across the wonderful “out-htmlview”. And coming across this post by Alexander. I was able to finally achieve a report that is comprehensive, tidy and […]

akane
akane
4 years ago

Thank you so much for this.