O R G A N I C / F E R T I L I Z E R: list domain controllers with powershell

Oct 6, 2009

list domain controllers with powershell

for my own edification and later reference.

 
to start, let's grab the current domain.
$myDomain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()

 
just for fun, we’ll look at the forest.
$myDomain.Forest

 
now, let’s list the domains of the forest.
$myDomain.Forest.Domains

 
this will count every domain controller in every domain.
$myDomain.Forest.Domains | % { $_.DomainControllers.Count }

 
for a final count, we’ll add all the numbers together into $myCount.
$myDomain.Forest.Domains | % { $myCount = $_.DomainControllers.Count + $myCount}

 
to list all of the domain controllers, we can run this command.
$myDomain.Forest.Domains | % { $_.DomainControllers } | Select-Object name

 
finally, another way to count all of the domain controllers in the forest.
($myDomain.Forest.Domains | % { $_.DomainControllers } | Select-Object name).count

3 comments:

  1. Great stuff! Its helped me a lot!. I am stuck on something thought - if I can pick your brain. Using strings such as yours; how could I dump a listing of all domains in a forest, sites within those domains, domain controllers at those sites, and the subnet ranges of the sites themselves. I've seen bits and pieces around here and there - but I'm not smart enough to figure out how to get them together just right so that everything correlates in a nice csv or something I can actually work with.... Any ideas?

    ReplyDelete
  2. for some reason, i'm just now seeing this comment, josh. i'll put something together and post it today.

    ReplyDelete
  3. hal's version: http://halr9000.com/article/433

    ReplyDelete