List domain controller count using PowerShell

Viewed 1003

I'm trying to get the number (count) of domain controllers in the domain. See below example code:

Import-Module ActiveDirectory -Force
(Get-ADDomainController -Filter "*" | Select-Object Name).Count
2

However when running the command in a domain with a single domain controller (count 1) there is no output.

(Get-ADDomainController -Filter "*" | Select-Object Name).Count

No output. Why is the 'Count' not shown as 1 with a single DC? What am I missing?

1 Answers

Try this:

$tc = 0
(Get-ADForest).Domains | ForEach {
 $tc += (Get-ADDomain -Identity $_ | Select-Object -ExpandProperty ReplicaDirectoryServers).Count
}
$tc
  • Get the domains of AD forest.

  • Get The Replica Domain Controllers on each domain and add their count to the $tc variable.

  • Return total count.

Related