← SCRIPT LIBRARY

Active Directory · READ-ONLY / LOCAL CSV · v1.0.0

Export standard AD group attributes

Export standard group metadata for selected OUs, with duplicate groups removed and Notes off by default.

Lab validation pending

Offline tests do not establish live directory behavior. Review the source and run first in a small authorized lab. Nothing runs in this browser.

What you’ll need

  • PowerShell on Windows with the ActiveDirectory (RSAT) module.
  • Read access to the selected objects and required attributes on the chosen domain controller.

Run locally

Replace fictional inputs with values approved for your environment. Use an access-controlled local folder for output.

./Export-SwpADGroupAttributes.ps1 -Server dc01.example.test -SearchBase 'OU=Groups,DC=example,DC=test' -OutputPath ./group-attributes.csv

Read the results

FieldMeaning
SamAccountNameThe supplied user or returned group name.
CreatedUtcObject creation time in UTC, when returned.
ChangedUtcLast recorded object change time in UTC, when returned. Not a sign-in time.
DescriptionThe visible standard description field.
MailThe visible standard mail field.
NotesIncluded only when -IncludeNotes is selected.
  • Separate multiple search bases with commas. The export deduplicates overlaps by object GUID.
  • Uses standard fields only. Do not include Notes without approval to store that text in the local report.
  • A query failure stops the report; zero visible groups creates no CSV.

Keep the output local and protected. A successful report does not prove complete directory visibility; do not use it by itself to change or remove accounts or groups.

Formula-like text cells are prefixed with an apostrophe. Keep this in mind when joining exports by identifier.

Complete source

The display, clipboard copy, download and GitHub link refer to the same pinned source.

Export-SwpADGroupAttributes.ps1POWERSHELL / READ ONLY
<#
.SYNOPSIS
Exports standard group attributes under explicitly selected OUs.
.DESCRIPTION
Uses one specified DC. Overlapping search bases are deduplicated by GUID.
Uses only standard AD attributes. Add -IncludeNotes only if the local
report is approved to contain Notes text. No directory writes. A query
failure aborts before export; a zero-result run creates no file.
Version 1.0.0.
.EXAMPLE
./Export-SwpADGroupAttributes.ps1 -Server dc01.example.test -SearchBase 'OU=Groups,DC=example,DC=test' -OutputPath ./group-attributes.csv
#>
[CmdletBinding()]
param(
    [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [string] $Server,
    [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [string[]] $SearchBase,
    [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [string] $OutputPath,
    [switch] $IncludeNotes
)
$ErrorActionPreference = 'Stop'
Import-Module ActiveDirectory
function Protect-Cell([string] $Value) {
    if ($Value -match '^[\s\x00-\x1f]*[=+@-]|^[\t\r\n]') { return "'" + $Value }
    return $Value
}
if (Test-Path -LiteralPath $OutputPath) { throw 'Choose a new output file.' }
$fields = @('whenCreated', 'whenChanged', 'description', 'mail')
if ($IncludeNotes) { $fields += 'info' }
$seen = [System.Collections.Generic.HashSet[guid]]::new()
$rows = @(foreach ($base in ($SearchBase | Select-Object -Unique)) {
    if ([string]::IsNullOrWhiteSpace($base)) { throw 'Search bases cannot be blank.' }
    Get-ADGroup -Filter * -SearchBase $base -SearchScope Subtree -Server $Server -ResultPageSize 200 -Properties $fields | ForEach-Object {
        if (!$_.ObjectGUID) { throw 'A returned group is missing its object GUID.' }
        if ($seen.Add($_.ObjectGUID)) {
            $row = [ordered]@{
                SamAccountName = Protect-Cell $_.SamAccountName
                CreatedUtc = if ($null -ne $_.whenCreated) { $_.whenCreated.ToUniversalTime().ToString('o') }
                ChangedUtc = if ($null -ne $_.whenChanged) { $_.whenChanged.ToUniversalTime().ToString('o') }
                Description = Protect-Cell $_.Description
                Mail = Protect-Cell $_.Mail
            }
            if ($IncludeNotes) { $row['Notes'] = Protect-Cell $_.Info }
            [pscustomobject]$row
        }
    }
})
if (!$rows.Count) { Write-Host 'No visible groups. No CSV written.'; return }
$rows | Export-Csv -LiteralPath $OutputPath -NoTypeInformation -Encoding UTF8 -NoClobber
Write-Host ('Exported {0} unique group(s).' -f $rows.Count)

Microsoft references