We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Microsoft Teams Report using Powershell

Medium Priority
53 Views
Last Modified: 2020-06-05
Hi Guys,

I have a requirement where I need to obtain a report of all the Teams that are in our org with the details of those teams, specifically:
Team Name, Members, Owners, User Count and Creation Date

I have a script, but it doesn't work on the network or even outside of it - https://techcommunity.microsoft.com/t5/microsoft-teams/microsoft-teams-tenant-wide-csv-report/m-p/151875#

This script works fine, but it doesn't export all the details I need - https://www.jijitechnologies.com/blogs/export-microsoft-teams-details-with-members

I need some assistance with Powershell, can someone help me?
Comment
Watch Question

FOXActive Directory/Exchange Engineer
CERTIFIED EXPERT
Top Expert 2015

Commented:
For the script that you have run this line first before running your script
Set-ExecutionPolicy Bypass


REITSenior Cloud and Infrastructure Engineer
CERTIFIED EXPERT

Author

Commented:
The one from the Microsoft blog?
CERTIFIED EXPERT
Top Expert 2014

Commented:
Changing the execution policy would only help in limited circumstances.  I don't recommend setting it to "Bypass" unless you understand the ramifications.

Running the script in the first link works for me and provides the details you asked for, though as it notes you have to first have the modules for Teams and Exchange Online loaded.  "Doesn't work" is not near enough info to help you though, so you need to state if you're getting any errors, what you are seeing, etc.

A thing to note about the script is that it is horribly inefficient, so I've rewritten it to remove unnecessary and redundant queries.  Requirements for the modules remains the same.
$outPath = 'c:\temp'
If ( !(Test-Path -Path $outPath) )
{ New-Item -ItemType Directory -Path $outPath | Out-Null }
$outFile = "TeamsData_$(Get-Date -Format "yyyy-MM-dd_HHmm").csv"
Write-Host "Data output to file ""$outFile"""

Get-Team | ForEach-Object `
{
    $team = $_
    $membership = Get-TeamUser -GroupId $team.GroupId
    $team | Select DisplayName,
                   @{n="Members";e={($membership | Where-Object { $_.Role -ne "Owner" }).User -join "; "}},
                   @{n="Owners";e={($membership | Where-Object { $_.Role -eq "Owner" }).User -join "; "}},
                   @{n="UserCount";e={$membership.Count}},
                   @{n="CreationDate";e={(Get-UnifiedGroup -identity $team.GroupId).WhenCreated}}
} | Export-Csv "$(Join-Path $outPath $outFile)" -NoTypeInformation
Some clarifying questions for adjustments to the code.
  1. Should "members" include only members who aren't owners, or all members?  What about guests?
  2. Should members and owners be listed by AzureAD id (typically their email) or their name?
  3. Should user count include owners, or just those explicity set as "members"? What about guests?
REITSenior Cloud and Infrastructure Engineer
CERTIFIED EXPERT

Author

Commented:
That works beautifully! Is there any chance amending it to include the 'TeamType' and 'TeamGuests'?

I think it would be an addition to line 12?
CERTIFIED EXPERT
Top Expert 2014

Commented:
Sure, if you can provide some answers to my 3 questions above.  Not really required for the additions, but I don't want to be making changes multiple times.
REITSenior Cloud and Infrastructure Engineer
CERTIFIED EXPERT

Author

Commented:
Sure, missed that part!

  1. Should "members" include only members who aren't owners, or all members?  What about guests? - Members and Owners, can guests be shown on a separate tab?
  2. Should members and owners be listed by AzureAD id (typically their email) or their name? - Azure Id would be fine, providing this is their UPN or email address
  3. Should user count include owners, or just those explicity set as "members"? What about guests? - Include all

CERTIFIED EXPERT
Top Expert 2014
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
REITSenior Cloud and Infrastructure Engineer
CERTIFIED EXPERT

Author

Commented:
Worked great, thankyou!
REITSenior Cloud and Infrastructure Engineer
CERTIFIED EXPERT

Author

Commented:
Hi chaps,

The script was working fine, but when I run the script now, I get the attached, any ideas why?
FOXActive Directory/Exchange Engineer
CERTIFIED EXPERT
Top Expert 2015

Commented:
Did you log back in to your online session?  the session will automatically time out after a while
REITSenior Cloud and Infrastructure Engineer
CERTIFIED EXPERT

Author

Commented:
Yeah I logged back in, script hangs for a while and then re-prompts me
FOXActive Directory/Exchange Engineer
CERTIFIED EXPERT
Top Expert 2015

Commented:
try exiting out of the powershell box and start off fresh
REITSenior Cloud and Infrastructure Engineer
CERTIFIED EXPERT

Author

Commented:
Tried it, does the same thing. I'm doing this on my personal machine
FOXActive Directory/Exchange Engineer
CERTIFIED EXPERT
Top Expert 2015

Commented:
When you ran it the other day it was on a different computer?

Did you set the execution policy to

Set-ExecutionPolicy Bypass
CERTIFIED EXPERT
Top Expert 2014

Commented:
Execution policy doesn't come into play here.  I actually consider setting the execution policy to Bypass to be a security vulnerability.  I advocate using RemoteSigned, so that you are warned when you try to run a script file downloaded from the internet.  If you trust the source or have examined the file so you know it doesn't contain anything malicious, then you can unblock the file.
<end soapbox>

The prompt has to do with the Exchange Online cmdlets, so how are you connecting to Exchange Online?

The Get-UnifiedGroup cmdlet runs against Exchange Online.  Note I've only tested with the older method of connecting to ExO, and not with the new ExO v2 module.
REITSenior Cloud and Infrastructure Engineer
CERTIFIED EXPERT

Author

Commented:
The account i'm running the script with has GA rights on Office 365, it also has MFA enabled.

I use the following at the start of the script to connect to Exchange Online
Write-Host "Checking for an existing connection to Exchange Online..." -ForegroundColor Yellow


$exSession = Get-PSSession | Where {$_.ComputerName -match "outlook.office365.com" -and $_.State -match "Opened"}
    if ($exSession.Count -ge 1){
        Write-Host "Connected!" -ForegroundColor Green
        }
   
    else {
        #Connect to Exchange Online
        Write-Host "Connecting to Exchange Online" -ForegroundColor Green
        Import-Module $((Get-ChildItem -Path $($env:LOCALAPPDATA+"\Apps\2.0\") -Filter Microsoft.Exchange.Management.ExoPowershellModule.dll -Recurse ).FullName|?{$_ -notmatch "_none_"}|select -First 1)
        $EXOSession = New-ExoPSSession -UserPrincipalName $UserCredential.UserName
        Import-PSSession $EXOSession
        }
""

I then connect to Microsoft Teams using
Import-Module MicrosoftTeams
Connect-MicrosoftTeams
CERTIFIED EXPERT
Top Expert 2014

Commented:
Perhaps it's due to how you're importing the module, or maybe you have an old version.  The modules should auto-load.  In a new PS console, try just
Connect-ExchangeOnline
#then
Connect-MicrosoftTeams
Then run the script.

Some of your code above seems to be a holdover from the older method of connecting to Exchange Online, particularly the Import-PSSession $EXOSession command.  I would make sure your module is updated and follow the instructions as on https://docs.microsoft.com/en-us/powershell/exchange/exchange-online/exchange-online-powershell-v2/exchange-online-powershell-v2?view=exchange-ps
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.