forked from 12Knocksinna/Office365itpros
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFind-TeamsForUser.PS1
More file actions
68 lines (58 loc) · 3.96 KB
/
Find-TeamsForUser.PS1
File metadata and controls
68 lines (58 loc) · 3.96 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
# Find-TeamsForUser.PS1
# V1.0 26-Feb-2026
# An example of using the Microsoft Graph PowerShell SDK to generate a report of Teams memberships for selected users.
# GitHub Link: https://github.com/12Knocksinna/Office365itpros/blob/master/Find-TeamsForUser.PS1
Connect-MgGraph -NoWelcome -Scopes Group.Read.All, Groups.Read.All, Team.ReadBasic.All
# The group that defines the users to report on. Make sure that you change this for your tenant.
$GroupId = (Get-MgGroup -Filter "displayName eq 'Users to Monitor for Teams Membership'").Id
# If you want to process all users, make the change here to fetch all licensed users (accounts with assigned Teams licenses)
[array]$Users = Get-MgGroupMember -GroupId $GroupId
$Report = [System.Collections.Generic.List[Object]]::new()
ForEach ($User in $Users) {
Write-Host ("Fetching details of Teams membership for user {0} ({1}):" -f $User.additionalProperties.displayName, $User.additionalProperties.userPrincipalName) -ForegroundColor Yellow
Try {
[array]$Memberships = Get-MgUserJoinedTeam -UserId $User.Id -ErrorAction Stop| Sort-Object DisplayName
} Catch {
Write-Host ("Error fetching Teams memberships for {0}. Moving to next user" -f $User.additionalProperties.displayName) -ForegroundColor Red
Continue
}
If ($Memberships.Count -gt 0) {
Write-Host ("Found {0} Teams group memberships for {1}." -f $Memberships.Count, $User.additionalProperties.displayName) -ForegroundColor Green
ForEach ($Membership in $Memberships) {
$ReportLine = [PSCustomObject][Ordered]@{
UserId = $User.Id
User = $User.additionalProperties.displayName
UPN = $User.additionalProperties.userPrincipalName
'Job Title' = $User.additionalProperties.jobTitle
'Office' = $User.additionalProperties.officeLocation
'Team' = $Membership.DisplayName
'TeamId' = $Membership.Id
'Timestamp' = Get-Date -format 'dd-MMM-yyyy HH:mm:ss'
}
$Report.Add($ReportLine)
}
} Else {
Write-Host ("No Teams group memberships found for {0}." -f $User.additionalProperties.displayName) -ForegroundColor Yellow
}
}
Write-Host ""
# Generate the report in either Excel worksheet or CSV format, depending on if the ImportExcel module is available
If (Get-Module ImportExcel -ListAvailable) {
$ExcelGenerated = $True
Import-Module ImportExcel -ErrorAction SilentlyContinue
$ExcelOutputFile = ((New-Object -ComObject Shell.Application).Namespace('shell:Downloads').Self.Path) + "\Teams Memberships.xlsx"
$Report | Export-Excel -Path $ExcelOutputFile -WorksheetName "Teams Memberships" -Title ("Teams Memberships {0}" -f (Get-Date -format 'dd-MMM-yyyy')) -TitleBold -TableName "TeamsMemberships"
} Else {
$CSVOutputFile = ((New-Object -ComObject Shell.Application).Namespace('shell:Downloads').Self.Path) + "\Teams Memberships.CSV"
$Report | Export-Csv -Path $CSVOutputFile -NoTypeInformation -Encoding Utf8
}
If ($ExcelGenerated -eq $true) {
Write-Host ("Teams memberships report is available in Excel workbook {0}" -f $ExcelOutputFile)
} Else {
Write-Host ("Teams memberships report is available in CSV file {0}" -f $CSVOutputFile)
}
# An example script used to illustrate a concept. More information about the topic can be found in the Office 365 for IT Pros eBook https://gum.co/O365IT/
# and/or a relevant article on https://office365itpros.com or https://www.practical365.com. See our post about the Office 365 for IT Pros repository
# https://office365itpros.com/office-365-github-repository/ for information about the scripts we write.
# Do not use our scripts in production until you are satisfied that the code meets the needs of your organization. Never run any code downloaded from
# the Internet without first validating the code in a non-production environment.