All scripts
Microsoft 365 734

Get-EntraDynamicGroupRuleInventory

Exports every dynamic group's membership rule in plain text alongside its member count, so nobody has to decode a rule from memory to understand what a group actually targets.

Get-EntraDynamicGroupRuleInventory.ps1
<#
.SYNOPSIS
    Exports all dynamic group membership rules.

.DESCRIPTION
    Lists every dynamic membership group with its rule text and current
    member count, so nobody has to decode a rule from memory to
    understand what a group actually targets months after it was
    created.

.EXAMPLE
    .\Get-EntraDynamicGroupRuleInventory.ps1

.NOTES
    Requires Microsoft.Graph.Groups with an active Connect-MgGraph
    session.

.AUTHOR
    Shehryar Hassan
#>

$dynamicGroups = Get-MgGroup -Filter "groupTypes/any(c:c eq 'DynamicMembership')" -All

$report = foreach ($group in $dynamicGroups) {
    $memberCount = (Get-MgGroupMember -GroupId $group.Id -All).Count
    [pscustomobject]@{
        GroupName   = $group.DisplayName
        Rule        = $group.MembershipRule
        ProcessingState = $group.MembershipRuleProcessingState
        MemberCount = $memberCount
    }
}

$report | Format-Table GroupName, MemberCount, ProcessingState -AutoSize
$report | ForEach-Object { Write-Host "" ; Write-Host "$($_.GroupName): $($_.Rule)" -ForegroundColor Cyan }

Read it before you run it, and test in a safe tenant first.