-
Notifications
You must be signed in to change notification settings - Fork 15
/
15_Find_All_Microsoft_365_Group_Owners.ps1
72 lines (55 loc) · 2.16 KB
/
15_Find_All_Microsoft_365_Group_Owners.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
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
Set-Location C:\
Clear-Host
#To be able to install the exchange online module
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
#Install the exchange online management module
Install-Module -Name ExchangeOnlineManagement -Verbose -Force
#Update the module (if necessary)
Update-Module -Name ExchangeOnlineManagement -Verbose -Force
#Connect to exchange online
Connect-ExchangeOnline -UserPrincipalName admin365@tomscloud.onmicrosoft.com
#Get All Microsoft 365 Groups
$GroupData = @()
$Groups = Get-UnifiedGroup -ResultSize Unlimited -SortBy Name
#Loop through each Group
$Groups | Foreach-Object {
#Get Group Owners
$GroupOwners = Get-UnifiedGroupLinks -LinkType Owners -Identity $_.Id | Select DisplayName, PrimarySmtpAddress
$GroupData += New-Object -TypeName PSObject -Property @{
GroupName = $_.Alias
GroupEmail = $_.PrimarySmtpAddress
OwnerName = $GroupOwners.DisplayName -join "; "
OwnerIDs = $GroupOwners.PrimarySmtpAddress -join "; "
}
}
#Get Groups Data
$GroupData
$GroupData | Export-Csv "C:\Temp\GroupOwners.csv" -NoTypeInformation
#Remove the session
Disconnect-ExchangeOnline -Confirm:$false
##The second way##
#Get Credentials to connect
$Cred = Get-Credential
#We need the cmdlets
Install-Module -Name AzureAD -AllowClobber -Force -Verbose
#Sometimes the module must be imported
Import-Module AzureAD
#Connect to AzureAD
Connect-AzureAD -Credential $Cred | Out-Null
$GroupData = @()
#Get all Microsoft 365 Groups
Get-AzureADMSGroup -Filter "groupTypes/any(c:c eq 'Unified')" -All:$true | ForEach-object {
$GroupName = $_.DisplayName
#Get Owners
$GroupOwners = Get-AzureADGroupOwner -ObjectId $_.ID | Select UserPrincipalName, DisplayName
$GroupData += New-Object PSObject -Property ([Ordered]@{
GroupName = $GroupName
OwnerID = $GroupOwners.UserPrincipalName -join "; "
OwnerName = $GroupOwners.DisplayName -join "; "
})
}
#Export Group Owners data to CSV
$GroupData
$GroupData | Export-Csv "C:\Temp\GroupOwners.csv" -NoTypeInformation
#Remove the session
Disconnect-AzureAD