PowerShell Genie

Find duplicate mail addresses

Script 2 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn

Every user is retrieved with the mail attribute, accounts holding no address are dropped, Group-Object buckets the remainder by address, and only buckets with more than one member survive.

The script

1PS> Get-ADUser -Filter * -Properties mail | Where-Object mail | Group-Object mail | Where-Object Count -gt 1 | Select-Object Name,Count

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 Name Count
    2 ---- -----
    3 alex@contoso.example 2
    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-ADUser -Filter * -Properties mail | Where-Object mail | Group-Object mail | Where-Object Count -gt 1 | Select-Object Name,Count
    1. Stage 1Get-ADUser-Filter -Properties
    2. Stage 2Where-ObjectPositional values or no named parameters
    3. Stage 3Group-ObjectPositional values or no named parameters
    4. Stage 4Where-Object-gt
    5. Stage 5Select-ObjectPositional values or no named parameters

      How it works

      Every user is retrieved with the mail attribute, accounts holding no address are dropped, Group-Object buckets the remainder by address, and only buckets with more than one member survive. In the output, Name is the offending address and Count is how many accounts claim it.

      Adapting it

      Request proxyAddresses instead and group on the expanded values, because in a hybrid directory the collisions that actually break synchronization usually live there rather than in the primary mail attribute.

      Requirements

      ActiveDirectory module.

      If the result is unexpected

      The duplicate-mail report groups accounts that should not share an address, or misses an alias conflict.

      Grouping the mail attribute does not inspect every proxy address and blank values can form a misleading group.

      1. Exclude empty mail values and inspect each group before comparing aliases in the appropriate attribute.

      Validate the result

      Each reported group contains at least two distinct account identities sharing the compared value.

      Keep the check reliable

      Document whether the audit covers primary mail values, aliases or both.

      Primary documentation

      PowerShell objects and pipelines documentation

      Continue
      Genie guideFind duplicate mail addresses

      Every user is retrieved with the mail attribute, accounts holding no address are dropped, Group-Object buckets the remainder by address, and only buckets with more than one member survive.