Forum Discussion
gcjuw84
Apr 16, 2020Brass Contributor
Get all Teams and channel information using PowerShell
Hi everyone I'm trying to export a list of all teams in our environment along with their associated channels to CSV. I have used the script below previously (although this was around a year ago)...
- Apr 16, 2020
Hi gcjuw84
Here is the link of commands for your reference.
Export all the Teams Channel in Office 365 Tenant
https://gallery.technet.microsoft.com/office/Export-Teams-Channel-de703ad0
With Regards,
Satish U
gcjuw84
May 19, 2020Brass Contributor
No problem, the final script is below. Before running the script you need to make sure you run Connect-MicrosoftTeams and Connect-ExchangeOnline.
Any problems let me know.
$AllTeamsInOrg = (Get-Team).GroupID
$TeamList = @()
Write-Output "This may take a little bit of time... Please sit back, relax and enjoy some GIFs inside of Teams!"
Write-Host ""
Foreach ($Team in $AllTeamsInOrg)
{
$TeamGUID = $Team.ToString()
$TeamGroup = Get-UnifiedGroup -identity $Team.ToString()
$TeamName = (Get-Team | ?{$_.GroupID -eq $Team}).DisplayName
$TeamOwner = (Get-TeamUser -GroupId $Team | ?{$_.Role -eq 'Owner'}).User
$TeamUserCount = ((Get-TeamUser -GroupId $Team).UserID).Count
$TeamGuest = (Get-UnifiedGroupLinks -LinkType Members -identity $Team | ?{$_.Name -match "#EXT#"}).Name
if ($TeamGuest -eq $null)
{
$TeamGuest = "No Guests in Team"
}
$TeamChannels = (Get-TeamChannel -GroupId $Team).DisplayName
$ChannelCount = (Get-TeamChannel -GroupId $Team).ID.Count
$TeamList = $TeamList + [PSCustomObject]@{TeamName = $TeamName; TeamObjectID = $TeamGUID; TeamOwners = $TeamOwner -join ', '; TeamMemberCount = $TeamUserCount; NoOfChannels = $ChannelCount; ChannelNames = $TeamChannels -join ', '; SharePointSite = $TeamGroup.SharePointSiteURL; AccessType = $TeamGroup.AccessType; TeamGuests = $TeamGuest -join ','}
}
#######
$TestPath = test-path -path 'c:\temp'
if ($TestPath -ne $true) {New-Item -ItemType directory -Path 'c:\temp' | Out-Null
write-Host 'Creating directory to write file to c:\temp. Your file is uploaded as TeamsDatav2.csv'}
else {Write-Host "Your file has been uploaded to c:\temp as 'TeamsDatav2.csv'"}
$TeamList | export-csv c:\temp\TeamsDatav2.csv -NoTypeInformationSebCerazy
Feb 06, 2023Iron Contributor
After 10 hours of running the script, eventually all I got is:
Write-ErrorMessage : The underlying connection was closed: A connection that was expected to be kept alive was closed by the server.
Write-ErrorMessage : The underlying connection was closed: A connection that was expected to be kept alive was closed by the server.
- EnviableOneFeb 17, 2023Copper Contributor90% of the time, its better to filter on the server side, this might work a little faster, and will give you some feedback:
$clock = [System.Diagnostics.Stopwatch]::StartNew()
write-host "Connecting to MS and loading teams please wait..." -NoNewline
$AllTeamsInOrg = Get-Team
$Teamcount = $AllTeamsInOrg.count
write-host "Teams loaded ("
$TeamList = @()
$i = 0
Write-Host "This may take a little bit of time... Please sit back, relax and enjoy some GIFs inside of Teams!"
Foreach ($Team in $AllTeamsInOrg){
$i++
Write-host "Processing Team $i of $teamcount ($($team.displayname)) ..." -NoNewline -ForegroundColor Gray
$TeamGUID = $Team.groupid
$TeamGroup = Get-UnifiedGroup -identity $Team.GroupId
$TeamName = $Team.DisplayName
$TeamOwner = (Get-TeamUser -GroupId $Team.GroupId -Role Owner).User
$TeamUserCount = (Get-TeamUser -GroupId $Team.GroupId).Count
$TeamGuest = (Get-TeamUser -GroupId $Team.GroupId -Role Guest).Name
if ($TeamGuest -eq $null){
$TeamGuest = "No Guests in Team"
}
$TeamChannels = (Get-TeamChannel -GroupId $Team.GroupId).DisplayName
$ChannelCount = (Get-TeamChannel -GroupId $Team.GroupId).Count
$TeamList += [PSCustomObject]@{TeamName = $TeamName; TeamObjectID = $TeamGUID; TeamOwners = $TeamOwner -join ', '; TeamMemberCount = $TeamUserCount; NoOfChannels = $ChannelCount; ChannelNames = $TeamChannels -join ', '; SharePointSite = $TeamGroup.SharePointSiteURL; AccessType = $TeamGroup.AccessType; TeamGuests = $TeamGuest -join ','}
Write-host "done." -ForegroundColor Green
}
#######
$pathtest = test-path -path 'c:\temp'
if ($pathtest) {New-Item -ItemType directory -Path 'c:\temp' | Out-Null
write-Host 'Creating directory to write file to c:\temp.'
}
$TeamList | export-csv c:\temp\TeamsDatav2.csv -NoTypeInformation
$clock.Stop()
Write-Host "Completed. Written to: 'c:\temp\TeamsDatav2.csv'" -ForegroundColor Yellow
Write-Host "$i Teams Loaded, Time taken $($clock.elapsed.ToString("G"))" -ForegroundColor Yellow