Skip to content

Instantly share code, notes, and snippets.

@joerodgers
Last active November 20, 2024 14:52
Show Gist options
  • Save joerodgers/4657a51aa68766aa6481e497ec0419c3 to your computer and use it in GitHub Desktop.
Save joerodgers/4657a51aa68766aa6481e497ec0419c3 to your computer and use it in GitHub Desktop.
Example of using the Microsoft.Graph PowerShell module to read basic metadata about all unified groups and dump results to csv.
#requires -modules "Microsoft.Graph.Authentication", "Microsoft.Graph.Groups", "Microsoft.Graph.Files"
<#
App Principal Permissions Required, must have any one permission for each of the three cmdlets.
Get-MgGroup == GroupMember.Read.All, Group.Read.All, Directory.Read.All, Group.ReadWrite.All, Directory.ReadWrite.All
Get-MgGroupOwner == GroupMember.Read.All, Group.Read.All, GroupMember.ReadWrite.All, Group.ReadWrite.All, Directory.Read.All
Get-MgGroupDrive == Files.Read.All, Files.ReadWrite.All, Sites.Read.All, Sites.ReadWrite.All
#>
# connect to graph
Connect-MgGraph `
-ClientId $env:O365_CLIENTID `
-CertificateThumbprint $env:O365_THUMBPRINT `
-TenantId $env:O365_TENANTID | Out-Null
Select-MgProfile -Name beta # required for Get-MgGroupEndpoint
# query for all unified groups
$unifiedGroups = Get-MgGroup -Filter "groupTypes/any(a:a eq 'unified')" -Property Id, DisplayName, CreatedDate, Mail, Visibility, CreatedDateTime, resourceProvisioningOptions -ExpandProperty Owners -PageSize 999 -All
$timestamp = Get-Date -Format FileDateTime
$counter = 1
# enumerate unified groups
foreach( $unifiedGroup in $unifiedGroups )
{
Write-Host "$(Get-Date) - Processing $($counter.ToString("D4")) of $($unifiedGroups.Count) groups"
$owners = $unifiedGroup.Owners.AdditionalProperties.userPrincipalName -join ","
# pull the drive information
$drive = Get-MgGroupDrive -GroupId $unifiedGroup.Id -Property SharePointIds | Select-Object -First 1
# pull endpoints
$groupEndpoints = Get-MgGroupEndpoint -GroupId $unifiedGroup.Id
$group = [PSCustomObject] @{
GroupId = $unifiedGroup.Id
GroupName = $unifiedGroup.DisplayName
CreatedDateTime = $unifiedGroup.CreatedDateTime
Mail = $unifiedGroup.Mail
Visibility = $unifiedGroup.Visibility
TeamsConnected = $unifiedGroup.AdditionalProperties.resourceProvisioningOptions -contains "Team"
YammerConnected = ($groupEndpoints | Where-Object -Property "ProviderName" -eq "Yammer") -ne $NULL
WebUrl = $drive.SharePointIds.SiteUrl
Owners = $owners
}
$group | Export-Csv -Path "C:\_temp\UnifiedGroupInformation_$timestamp.csv" -NoTypeInformation -Append
$counter++
}
@azureskydiver
Copy link

If you are using newer version of the MSGraph SDK, skip the Select-MgProfile -Name beta line, and instead install the module Microsoft.Graph.Beta.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment