Listing Active Domain Controller

by May 9, 2023

If your machine is connected to a domain, you can use PowerShell to identify the domain controller you are connected to. Either use this command:

 
Get-ADDomainController -Discover 
 

Or, simply look up the “LOGONSERVER” environment variable:

 
$env:LOGONSERVER    
 

It lists the name of the computer that you logged on to. If it is equal to your own computer name (without the backslashes), then you are logged on locally and not to a domain:

if ($env:LOGONSERVER.TrimStart('\') -eq $env:COMPUTERNAME)
{
    "local"
}
else
{
    "logged on to $env:LOGONSERVER"
}


Tweet this Tip! Tweet this Tip!