-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExport-M365SharedChannelsGuestMembers.ps1
More file actions
81 lines (65 loc) · 2.64 KB
/
Export-M365SharedChannelsGuestMembers.ps1
File metadata and controls
81 lines (65 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<#
.SYNOPSIS
Exports Microsoft Teams shared channels and their external (guest) members to an Excel file.
.DESCRIPTION
Connects to Microsoft Teams, finds all shared channels across all teams, and retrieves guest members' display names and email addresses.
Outputs the results to an Excel file using the ImportExcel PowerShell module.
.PARAMETER OutputPath
Optional. Full path to the output Excel file. Defaults to "C:\Temp\SharedChannels.xlsx".
.EXAMPLE
.\Export-M365SharedChannelsGuestMembersToExcel.ps1 -OutputPath "D:\Reports\SharedGuests.xlsx"
.NOTES
- Requires the MicrosoftTeams and ImportExcel modules.
- Run with sufficient Teams admin permissions.
#>
[CmdletBinding()]
param (
[string]$OutputPath = "C:\Temp\SharedChannels.xlsx"
)
# Ensure output directory exists
$OutputDirectory = Split-Path -Path $OutputPath -Parent
if (-not (Test-Path $OutputDirectory)) {
New-Item -Path $OutputDirectory -ItemType Directory -Force | Out-Null
}
# Install MicrosoftTeams if not already installed
if (-not (Get-Module -ListAvailable -Name MicrosoftTeams)) {
Install-Module -Name MicrosoftTeams -Scope CurrentUser -Force
}
Import-Module MicrosoftTeams
# Install ImportExcel if not already installed
if (-not (Get-Module -ListAvailable -Name ImportExcel)) {
Install-Module -Name ImportExcel -Scope CurrentUser -Force
}
Import-Module ImportExcel
# Connect to Microsoft Teams
try {
Connect-MicrosoftTeams -ErrorAction Stop
} catch {
Write-Error "❌ Failed to connect to Microsoft Teams: $_"
return
}
# Retrieve all Teams
$teams = Get-Team
# Prepare result array
$sharedChannelGuests = @()
foreach ($team in $teams) {
# Get shared channels
$sharedChannels = Get-TeamChannel -GroupId $team.GroupId | Where-Object { $_.MembershipType -eq "Shared" }
foreach ($channel in $sharedChannels) {
# Get members of the shared channel
$members = Get-TeamChannelUser -GroupId $team.GroupId -DisplayName $channel.DisplayName
# Filter and format guest users
$guestUsers = $members | Where-Object { $_.UserType -eq "Guest" }
foreach ($guest in $guestUsers) {
$sharedChannelGuests += [PSCustomObject]@{
TeamName = $team.DisplayName
SharedChannelName = $channel.DisplayName
GuestDisplayName = $guest.Name
GuestEmail = $guest.User
}
}
}
}
# Export to Excel
$sharedChannelGuests | Export-Excel -Path $OutputPath -WorksheetName "GuestMembers" -BoldTopRow -AutoSize -AutoFilter -FreezeTopRow
Write-Host "✅ Excel report saved to: $OutputPath"