Reading Time: 2 minutes

When managing an Active Directory (AD) environment, keeping track of all domain controllers (DCs) in the domain is important. This information can be helpful for various purposes, such as troubleshooting, monitoring, or auditing. This blog post will show you how to use PowerShell one-liners to quickly and easily list all DCs in an AD domain.

First and foremost, you need to ensure that the following module is present and loaded.

# Check if the ActiveDirectory module is Loaded
Get-Module -Name ActiveDirectory

# Check if the ActiveDirectory module is available
Get-Module -Name ActiveDirectory -ListAvailable

# Import the ActiveDirectory module if it is not loaded
Import-Module -Name ActiveDirectory

List of all the Domain Controllers in the AD Domain

This one-liner retrieves information about all DCs in the current domain. The output includes the name, site, operating system version, and other details of each DC.

Get-ADDomainController

Display the result in a grid view window.

Get-ADDomainController| out-GridView

Display the total number of Domain Controllers that currently exist in your environment.

Get-ADDomainController | Measure-Object

Export the List to a CSV File

Finally, we need to export the list to a CSV file. This allows us to easily share the list with other team members or import it into other tools. This command is similar to the previous one, but we added the Export-Csv cmdlet to save the output to a CSV file. The -Path parameter specifies where to save the file and the -NoTypeInformation parameter removes the #TYPE line from the CSV file.

Get-ADDomainController | Export-Csv -Path "$home\Desktop\ADServers.csv" -NoTypeInformation

With these simple commands, you can quickly retrieve valuable information about your AD environment. Whether you’re a sysadmin, helpdesk technician, or security analyst, this knowledge can help you better manage and secure your infrastructure.

More posts on PowerShell and Active Directory:

Thanks for reading my blog!

Feel free to drop your comment or question below.