Get-Teams does not find all teams

I have this powershell script where I want to fetch all teams and users and store it into a CSV file. it works perfectly for some of the teams, but for 90% of the teams the script throws an error “Cannot bind argument to parameter ‘GroupId’ because it is null”. When provided with a -DisplayName filter for a specific team, the script will find all users in the team without issue but without the filter it will not find that team.

$Result = @()
#Get all teams
$AllTeams= Get-Team -NumberOfThreads 10
$TotalTeams = $AllTeams.Count
$i = 0
#Iterate teams one by one and get channels 
ForEach ($Team in $AllTeams)
    Write-Progress -Activity "Fetching users for $($Team.Displayname)" -Status "$i out of $TotalTeams completed"
    #Get team users
    $TeamUsers = Get-TeamUser -GroupId $Team.GroupId
    #Iterate users one by one and add to the result array

    ForEach ($TeamUser in $TeamUsers)
            #Add user info to the result array
            $Result += New-Object PSObject -property $([ordered]@{
            TeamName = $Team.DisplayName
            TeamVisibility = $Team.Visibility
            UserName = $TeamUser.Name
            UserPrincipalName = $TeamUser.User
            Role = $TeamUser.Role
            GroupId = $Team.GroupId
            UserId = $Team.UserId
    Write-Host "Error occurred for $($Team.Displayname)" -f Yellow
    Write-Host $_ -f Red
#Export the result to CSV file
$Result | Export-CSV "C:\Temp\AllTeamUsersDoc.csv" -NoTypeInformation -Encoding UTF8

Is there some constraint for amount of calls per minute or something that I’m missing?

Welcome to the forum. :wave:t4:

Wihtout having experiences or access to a teams infrastructure …

How does it look when you run this snippet?

$AllTeams | 
    Select-Object -Property DisplayName, GroupId

Hi Olaf,

Thanks! Excited to be here :slight_smile:

Where exactly should I insert this snippet?
If I run this like so

$Result = @()
#Get all teams
$AllTeams= Get-Team -NumberOfThreads 20
$AllTeams | 
    Select-Object -Property DisplayName, GroupId
ForEach ($Team in $AllTeams)
    Write-Host $Team


I get a lot of “too many requests” errors.

Edit: I ran the script with 1 thread instead of 20, which returns a lot more teams than before. I guess the issue has been throttling all along. But even with 1 thread, not all teams are returned as expected. However, I don’t know how to control the speed of calls being executed through Get-Team. Any ideas?

That was not meant to be inserted in your script at all … it was just to check if the query $AllTeams= Get-Team -NumberOfThreads 20 returns all desired information. :wink:

Did you try to run it with omitting the parameter -NumberOfThreads?

Unfortunately not. Sorry. As I said I don’t have experiences wiht or access to a Teams infrastructure.

I see, thanks for trying though!
20 is the default value, so omitting it won’t help. I guess setting to 1 is my best guess now.
I’ll try making a workaround for now. Can’t believe I can’t find any information online about this issue.

But it won’t hurt though :wink:

MS does have some throttles. A search like “m365 powershell limit transactions concurrent calls” will return more information.

Microsoft 365 Resource Limits - Microsoft Service Assurance | Microsoft Learn

I would still give Olaf’s suggestion a try. Just confirm you are in fact getting all the teams. If you are getting all teams into a variable, then the rest of the script should just work.

$allTeams = get-team

keep it simple. then see if $allTeams holds all teams.

Microsoft knows there are some large companies out there, think 100k or more employees. So, unless you work for one of those, or in the process of migrating a larger on prem exchange or sharepoint to the cloud, that you are getting some data suggests you are not running into throttling.