PowerShell script to find the local Active Directory Controller


Here is a PowerShell script that you can use to find the local active directory domain controller in your environment, note that this script needs to be run on a computer that is joined to the Active Directory domain.


# Import the ActiveDirectory module

Import-Module ActiveDirectory


# Get the local domain controller

$localDC = Get-ADDomainController -Discover -DomainName (Get-ADDomain).Name


# Output the local domain controller

Write-Output "Local domain controller: $localDC"


This script first imports the ActiveDirectory module, which contains the cmdlet Get-ADDomainController that is used to retrieve the local domain controller. The -Discover parameter tells the cmdlet to search for the domain controller in the current domain, and the -DomainName parameter specifies the name of the domain to search in. Finally, the script outputs the local domain controller.


Note that this script assumes that the local computer is joined to an active directory domain. If it is not, then the Get-ADDomainController cmdlet will not be able to find the local domain controller. In that case, you will need to provide the domain controller's name or IP address manually.




Was this helpful?

Yes No


Comments