-
Notifications
You must be signed in to change notification settings - Fork 6
/
Get-AllTeamsUserIsPartOf.ps1
39 lines (30 loc) · 1.48 KB
/
Get-AllTeamsUserIsPartOf.ps1
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
# This script will retrieve all Teams a user is part of (including the role)
# MORE INFO on my Blog Post: https://veronicageek.com/powershell/powershell-for-o365/get-all-teams-a-user-is-part-of-using-powershell-pnp/2020/10/
####################################################################################################################################################
#Connect to Teams & Azure AD
Connect-PnPOnline -Scopes "Group.ReadWrite.All" -Credentials "<YOUR-CREDS-NAME>"
#Log file to export results
$logFile = "C:\users\$env:USERNAME\desktop\AllTeamsUserIn.csv"
#Store all the Teams
$allTeams = Get-PnPTeamsTeam
$results = @()
$userToFind = "[email protected]"
$userToFindInAD = Get-PnPAADUser | Where-Object ({ $_.UserPrincipalName -match $userToFind })
$userToFindID = $userToFindInAD.Id
#Loop through the TEAMS
foreach ($team in $allTeams) {
$allTeamsUsers = (Get-PnPTeamsUser -Team $team.DisplayName)
#Loop through users TARGETING THE USER ID TO MATCH
foreach ($teamUser in $allTeamsUsers) {
if ($teamUser.Id -match $userToFindID) {
$results += [pscustomobject]@{
userName = $userToFindInAD.UserPrincipalName
userDisplayName = $userToFindInAD.DisplayName
userRole = $teamUser.UserType
Team = $team.DisplayName
teamVisibility = $team.Visibility
}
}
}
}
$results | Export-Csv -Path $logFile -NoTypeInformation