Finding Domain Controllers

by May 16, 2012

If your computer is logged on to an Active Directory, here is some code to get to your domain controllers. Note that this will raise errors if you are currently not logged on to a domain.

PS> $Domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
PS> $Domain.DomainControllers

This lists all domain controllers. To find just one, try this:

$Domain.FindDomainController()

And to find all the domain controllers in your forest, simply retrieve all forest domains:

$Domain.Forest.Domains

Twitter This Tip! ReTweet this Tip!