PowerShell Tip: Get the AD site name of a computer

There are a few ways to get the AD site name of a computer.


This can be extremely useful if you want to base a script on the value of the computer’s site. Sometimes, however, you’ll want to query the site of remote computer.

Unfortunately, the ActiveDirectorySite class doesn’t allow that. One way to get the information is to query the DynamicSiteName registry value of the remote machine. The current site information is cached in the registry of a given machine under (HKLM:\SYSTEM\CurrentControlSet\services\Netlogon\Parameters).

Another is using the nltest command line utility:

nltest /server:server1 /dsgetsite

If the command completed successfully, we’ll have the site name in the first line. The last step is to wrap this into a function so we can reuse it later on.

function Get-ComputerSite($ComputerName)
   $site = nltest /server:$ComputerName /dsgetsite 2>$null
   if($LASTEXITCODE -eq 0){ $site[0] }


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 )

Google+ photo

You are commenting using your Google+ 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