PowerShell Genie

Audit privileged groups

Script 2 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn

An array holds the four group names worth watching, and the loop calls Get-ADGroupMember with -Recursive on each so nested membership is flattened into leaf members.

The script

1PS> $Groups = "Domain Admins" , "Enterprise Admins" , "Administrators" , "Schema Admins"; foreach($g in $Groups){ Get-ADGroupMember $g -Recursive | Select-Object @{ Name = "Group"; Expression = { $g } },SamAccountName,ObjectClass }

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.

    $Groups = "Domain Admins" , "Enterprise Admins" , "Administrators" , "Schema Admins"; foreach($g in $Groups){ Get-ADGroupMember $g -Recursive | Select-Object @{ Name = "Group"; Expression = { $g } },SamAccountName,ObjectClass }
    1. Stage 1$GroupsPositional values or no named parameters

      How it works

      An array holds the four group names worth watching, and the loop calls Get-ADGroupMember with -Recursive on each so nested membership is flattened into leaf members. A calculated property named Group stamps the loop variable onto every row, which keeps each result traceable to its source group once everything is combined.

      Adapting it

      Extend the array with Account Operators and any delegated admin groups your organization created, then pipe the whole thing into Export-Csv using a datestamped filename so successive runs build a change history.

      Requirements

      ActiveDirectory module plus rights to read privileged group membership; Enterprise Admins needs a forest root DC.

      If the result is unexpected

      A privileged-group report does not match the accounts that can perform an administrative action.

      Nested membership, domain scope and rights assigned outside the selected groups can alter effective access.

      1. Resolve the selected groups and inspect nested members while recording the queried domain.

      Validate the result

      Every row identifies its source group and the membership method used.

      Keep the check reliable

      Treat membership inventory as one input to access review rather than a complete effective-permissions calculation.

      Primary documentation

      PowerShell objects and pipelines documentation

      Continue
      Genie guideAudit privileged groups

      An array holds the four group names worth watching, and the loop calls Get-ADGroupMember with -Recursive on each so nested membership is flattened into leaf members.