How to get all Domain Controllers in Active Directory Forest with Powershell

22 January,2021 by Tom Collins

Question: I need to configure some applications to use some Domain Controllers . To establish the address - I need to list the Domain Controllers within the Active Directory Forest. Is there a method in Powershell to create a list of Domain Controllers ?

 

Answer: Powershell has a flexible group of Powershell cmdlets to get information about the Active Directory . Combining Get-ADForest and Get-ADDomainController  will create a recordset 

Here is a sample code snippet. This code will iterate through the Active Directory Forest  and discover the various Domains  :

 

(Get-ADForest).Domains | % { Get-ADDomainController -Discover -DomainName  $_ } | % { Get-ADDomainController -server $_.Name -filter * } | Select Name, Domain, Forest, IPv4Address, Site | ft

 

If you want just the Domain Controllers for the current domain  use this code (thanks to Harry Belafonte):

 

Get-ADDomainController -filter * | Select Name, Domain, Forest, IPv4Address, Site | ft

 

If you want to check for Domain Controllers of another domain - use this code. You'll need to supply a Domain server using -server filter

 

Get-ADDomainController -server myserver.net | Select Name, Domain, Forest, IPv4Address, Site | ft

 

Read more on Powershell and Active Directory 

How to find Active Directory users with Get-ADUser search filter

How to get the Active Directory groups membership for user with Powershell

How to Export Active Directory Group Members with Powershell Get-ADGroupMember

SQL Server – Powershell Active Directory search


Author: Tom Collins (http://www.sqlserver-dba.com)


Share:

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment on How to get all Domain Controllers in Active Directory Forest with Powershell


sqlserver-dba.com | SQL Server Performance Tuning | SQL Server DBA:Everything | FAQ | Contact|Copyright & Disclaimer