PowerShell Genie

Distribution group membership audit

Script 2 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn

The outer enumeration returns every distribution group, and for each one Get-DistributionGroupMember lists its members with its own unlimited result size.

The script

1PS> Get-DistributionGroup -ResultSize Unlimited | ForEach-Object { Get-DistributionGroupMember $_ -ResultSize Unlimited | Select-Object @{ Name = "Group"; Expression = { $_.Name} },Name,PrimarySmtpAddress }

Script reader

Inspect before you run

Inspection only

Inspect the script below. It does not run in this browser.

Inspection only — no command has run.

    Show reference output
    1 Group
    2 -----
    3 Domain Admins
    Inspect this example’s structure

    Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.

    Get-DistributionGroup -ResultSize Unlimited | ForEach-Object { Get-DistributionGroupMember $_ -ResultSize Unlimited | Select-Object @{ Name = "Group"; Expression = { $_.Name} },Name,PrimarySmtpAddress }
    1. Stage 1Get-DistributionGroup-ResultSize
    2. Stage 2ForEach-ObjectPositional values or no named parameters

      How it works

      The outer enumeration returns every distribution group, and for each one Get-DistributionGroupMember lists its members with its own unlimited result size. A calculated property named Group is meant to label each member row with the group it came from.

      Adapting it

      Capture the group object into a loop variable before the inner pipeline runs and reference that variable inside the calculated property, which is what makes the label point at the right thing.

      Requirements

      ExchangeOnlineManagement module with a Connect-ExchangeOnline session.

      If the result is unexpected

      A distribution-group membership export differs from mail-delivery expectations.

      Nested and dynamic membership behave differently from a direct static member list.

      1. Identify the group type and compare direct members with the membership model used for delivery.

      Validate the result

      The export labels direct static membership and does not claim dynamic evaluation where none occurred.

      Keep the check reliable

      Document whether nested groups or dynamic recipient filters are part of the review.

      Primary documentation

      PowerShell objects and pipelines documentation

      Continue
      Genie guideDistribution group membership audit

      The outer enumeration returns every distribution group, and for each one Get-DistributionGroupMember lists its members with its own unlimited result size.