PowerShell Genie

Find duplicate SPNs

Script 2 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn

Query Active Directory servicePrincipalName values, expand the collections, and group repeated SPNs to investigate duplicate registrations.

The script

1PS> Get-ADObject -LDAPFilter "(servicePrincipalName=*)" -Properties servicePrincipalName | ForEach-Object { $_.servicePrincipalName } | Group-Object | 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 HTTP/app01.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-ADObject -LDAPFilter "(servicePrincipalName=*)" -Properties servicePrincipalName | ForEach-Object { $_.servicePrincipalName } | Group-Object | Where-Object Count -gt 1 | Select-Object Name,Count
    1. Stage 1Get-ADObject-LDAPFilter -Properties
    2. Stage 2ForEach-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

      Get-ADObject with an LDAP filter matching any object that has a servicePrincipalName pulls the candidates, and ForEach-Object expands the multi-valued attribute so each individual SPN becomes its own pipeline item. Group-Object called with no property buckets identical strings together, and only buckets with a count above one are reported.

      Adapting it

      Include the owning object by grouping on a calculated pairing of the SPN with DistinguishedName, because knowing the duplicated SPN without knowing which two accounts hold it leaves the actual remediation unstarted.

      Requirements

      ActiveDirectory module.

      If the result is unexpected

      An SPN query reports duplicates or misses a suspected authentication conflict.

      The search scope or account types may omit registrations, and visually similar strings may not be identical.

      1. Inspect the exact SPN text and all returned account identities before changing any registration.

      Validate the result

      Each reported SPN maps to more than one account in the documented search scope.

      Keep the check reliable

      Manage SPN ownership through the service configuration and keep a record of changes.

      Primary documentation

      PowerShell objects and pipelines documentation

      Continue
      Genie guideFind duplicate SPNs

      Query Active Directory servicePrincipalName values, expand the collections, and group repeated SPNs to investigate duplicate registrations.