Export Domain Controller GUIDs Across Active Directory Forest Using PowerShell
Troubleshooting Active Directory replication issues becomes much easier when you can quickly identify which domain controller a GUID belongs to. This PowerShell script provides a reliable and scalable way to extract ObjectGUIDs from all domain controllers across your forest and export them to a CSV file for analysis or documentation. Whether you're resolving replication errors, cleaning up stale metadata, or auditing your AD infrastructure, this approach saves time and improves accuracy.
If you found this script helpful, consider bookmarking this page or sharing it with your fellow AD admins. Don’t forget to subscribe for more practical PowerShell solutions and Active Directory tips!
#############################Script Starts######################################
# Replace with your forest name
$forestName = "bbeninfotech.com"
$outputFile = "DC_GUIDs_Forest.csv"
# Create DirectoryContext for the forest
$context = New-Object System.DirectoryServices.ActiveDirectory.DirectoryContext("Forest", $forestName)
# Get the forest object
$forest = [System.DirectoryServices.ActiveDirectory.Forest]::GetForest($context)
$domains = $forest.Domains
# Initialize array to collect results
$results = @()
foreach ($domain in $domains) {
$domainName = $domain.Name
Write-Host "`n--- Domain: $domainName ---" -ForegroundColor Cyan
# Get all DCs in the domain
$dcs = Get-ADDomainController -Server $domainName -Filter *
foreach ($dc in $dcs) {
try {
# Use Name instead of HostName
$computer = Get-ADComputer $dc.Name -Server $domainName
$results += [PSCustomObject]@{
Domain = $domainName
DCName = $dc.Name
ObjectGUID = $computer.ObjectGUID
}
} catch {
Write-Warning "Failed to get ObjectGUID for $($dc.Name) in $domainName"
}
}
}
# Export to CSV
$results | Export-Csv -Path $outputFile -NoTypeInformation -Encoding UTF8
Write-Host "`nExported results to $outputFile" -ForegroundColor Green
#############################Script Ends######################################
Active Directory, PowerShell, Domain Controller, ObjectGUID, AD Forest, Replication Error, Repadmin, Metadata Cleanup, AD Troubleshooting, Export to CSV, Directory Services, Forest-Wide Script, AD Automation, IT Admin Tools, Windows Server, AD Health Check
Join the conversation