PowerShell – List all Domain Controllers

How to list all Domain controllers in PowerShell? There is no built-in cmdlet in the “Active Directory” module. I found the commands to list the domain controllers and wished to share them with others. So here they are.

First command with a super simple concept. Just list all objects in “Domain Controllers” OU.

Get-ADComputer -SearchBase 'OU=Domain Controllers,DC=us, DC=company,DC=com' -Filter *

The second command is to list all computer accounts with a UserAccountControl number that is specific to domain controllers.

Get-ADComputer -LDAPFilter "(&(objectCategory=computer)(userAccountControl:1.2.840.113556.1.4.803:=8192))"

The third command is a bit complicated. I needed domain controllers per site, so I used .NET object to get my list of domain controllers per site. I had to create new objects because the command results are not exportable to a CSV file if you need to.

[System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().Sites | Select-Object `
    Name, `
    @{Name="DomainControllers";Expression={$PSItem.Servers | ForEach-Object {$PSItem} }}, `
    @{Name="Networks";Expression={$PSItem.SubNets | ForEach-Object {$PSItem}}}, `
    @{Name="Domains";Expression={$PSItem.Domains | ForEach-Object{$PSItem}}}, `
    @{Name="SiteLinks";Expression={$PSItem.SiteLinks | ForEach-Object{$PSItem} }}, `
    @{Name="BridgeheadServers";Expression={$PSItem.BridgeheadServers | ForEach-Object {$PSItem} }}, `
    @{Name="PreferredRpcBridgeheadServers";Expression={$PSItem.PreferredRpcBridgeheadServers | ForEach-Object {$PSItem}}} 

I hope one of these commands got the information you needed.


One thought on “PowerShell – List all Domain Controllers

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s