← SCRIPT LIBRARY
DEFCON 4 · READ-ONLY / QUERY LOAD

Count Active Directory
groups by type.

Security. Distribution. Total. One paged query, without collecting the entire directory in memory.

RELEASE CANDIDATE — LIVE AD VALIDATION PENDING

This is complete source for review, not a claim of production validation. Test against a small lab OU before a domain-wide query.

Before you connect

The script queries a single domain controller and counts each group as it arrives. It does not expand membership or request every property. Domain-wide reads can still load a controller; choose your scope and timing deliberately.

The result reflects objects visible to your account during the query, not an atomic directory snapshot. An incomplete or failed query throws an error instead of printing a successful-looking partial total.

Start with a small scope

# Example DN only. Replace with your own lab OU.
.\Get-ADGroupTypeCount.ps1 -SearchBase "OU=Lab,DC=example,DC=com"

# Whole default domain; optional CSV must not already exist.
.\Get-ADGroupTypeCount.ps1 -CsvPath ".\GroupCounts.csv"

Without SearchBase, the default naming context on the selected controller is queried. Existing CSV files are never overwritten.

Complete source

Get-ADGroupTypeCount.ps1v1.0.0 / POWERSHELL
#requires -Version 5.1
#requires -Modules ActiveDirectory
<#
.SYNOPSIS
    Counts security and distribution groups using one paged AD query.
.DESCRIPTION
    Read-only against Active Directory. Groups are counted as they arrive;
    group objects and memberships are not accumulated in a collection.
    An optional CSV is a LOCAL write, never a directory modification.
    Large queries still consume domain-controller resources.
.PARAMETER Server
    Optional AD domain controller. If omitted, normal AD discovery is used.
.PARAMETER SearchBase
    Optional OU/container distinguished name. Defaults to the chosen
    controller's default naming context (not every domain in the forest).
.PARAMETER CsvPath
    Optional new CSV file. Its parent directory must exist. Never overwrites.
.EXAMPLE
    .\Get-ADGroupTypeCount.ps1 -SearchBase 'OU=Lab,DC=example,DC=com'
.EXAMPLE
    .\Get-ADGroupTypeCount.ps1 -Server 'dc01.example.com' -CsvPath '.\Counts.csv'
.NOTES
    Version: 1.0.0 / ShellWePlay preview
    Intended: Windows PowerShell 5.1; PowerShell 7 on Windows with AD module.
    Validation: Live Active Directory and compatibility testing PENDING.
    Permissions: Read access to the requested scope. No elevation required
    by the script itself; module installation is a separate prerequisite.
    Counts include only readable objects, not an atomic directory snapshot.
    Errors abort the result so partial counts are not reported as complete.
#>
[CmdletBinding()]
param(
    [ValidateNotNullOrEmpty()][string]$Server,
    [ValidateNotNullOrEmpty()][string]$SearchBase,
    [ValidateNotNullOrEmpty()][string]$CsvPath
)

Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
$started = Get-Date
$timer = [System.Diagnostics.Stopwatch]::StartNew()

try {
    Import-Module ActiveDirectory -ErrorAction Stop
    $rootArgs = @{ ErrorAction = 'Stop' }
    if ($PSBoundParameters.ContainsKey('Server')) { $rootArgs.Server = $Server }
    $root = Get-ADRootDSE @rootArgs
    # Pin all subsequent reads to the same controller.
    $controller = [string]$root.dnsHostName
    $domainBase = [string]$root.defaultNamingContext
    if ([string]::IsNullOrWhiteSpace($controller) -or
        [string]::IsNullOrWhiteSpace($domainBase)) {
        throw 'The selected controller did not return a default naming context.'
    }
    if (-not $PSBoundParameters.ContainsKey('SearchBase')) { $SearchBase = $domainBase }
    # Validate existence before starting the potentially expensive query.
    $null = Get-ADObject -Identity $SearchBase -Server $controller -ErrorAction Stop

    if ($PSBoundParameters.ContainsKey('CsvPath')) {
        $CsvPath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($CsvPath)
        if (Test-Path -LiteralPath $CsvPath) { throw "Output already exists: $CsvPath" }
        $parent = Split-Path -Path $CsvPath -Parent
        if (-not (Test-Path -LiteralPath $parent -PathType Container)) {
            throw "Output directory does not exist: $parent"
        }
    }

    # Mutable counters avoid retaining hundreds of thousands of AD objects.
    $counts = @{ Security = [long]0; Distribution = [long]0 }
    $query = @{
        Filter = '*'
        Server = $controller
        SearchBase = $SearchBase
        SearchScope = 'Subtree'
        ResultPageSize = 500
        ResultSetSize = $null
        ErrorAction = 'Stop'
    }
    Write-Verbose "Reading groups below $SearchBase from $controller"
    Get-ADGroup @query | ForEach-Object {
        switch ([string]$_.GroupCategory) {
            'Security' { $counts.Security++ }
            'Distribution' { $counts.Distribution++ }
            default { throw "Unrecognized category on group $($_.DistinguishedName). No totals emitted." }
        }
    }
    $timer.Stop()
    $result = [pscustomobject][ordered]@{
        Server = $controller
        SearchBase = $SearchBase
        Security = $counts.Security
        Distribution = $counts.Distribution
        Total = $counts.Security + $counts.Distribution
        StartedAt = $started.ToString('o')
        CompletedAt = (Get-Date).ToString('o')
        ElapsedSeconds = [math]::Round($timer.Elapsed.TotalSeconds, 2)
    }
    if ($PSBoundParameters.ContainsKey('CsvPath')) {
        $result | Export-Csv -LiteralPath $CsvPath -NoTypeInformation -Encoding UTF8 -NoClobber -ErrorAction Stop
    }
    # Emit the final result only after the entire operation succeeds.
    $result
}
catch {
    throw "Group count did not complete; no successful total was emitted. $($_.Exception.Message)"
}
finally {
    $timer.Stop()
}

Example result

Illustrative values, not measurements from a real directory.

Security       : 420
Distribution   : 80
Total          : 500
ElapsedSeconds : 2.7

Revision notes

1.0.0 — Initial review candidate. Paged, streaming counts; optional non-overwriting CSV; explicit failure on unrecognized group categories.

Reference

Microsoft: Get-ADGroup parameters and returned properties ↗