What are the different ways to get the current Active Directory (AD) site in PowerShell - and which is the fastest, most efficient, and works under the most scenarios?
There are a number of methods available:
(& "$env:SystemRoot\System32\nltest.exe" /DSGETSITE)[0]
[System.DirectoryServices.ActiveDirectory.ActiveDirectorySite]::GetComputerSite().Name
(Get-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\Netlogon\Parameters').DynamicSiteName
(Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine').'Site-Name'
(Get-CimInstance -ClassName Win32_NTDomain -Filter 'DomainControllerName != NULL' -Property ClientSiteName).ClientSiteName
[__ComObject].InvokeMember('SiteName', 'GetProperty', $null, (New-Object -ComObject ADSystemInfo), $null)
Options 1-3: https://powershellmagazine.com/2013/04/23/pstip-get-the-ad-site-name-of-a-computer/
Option 4: https://www.powershellgallery.com/packages/Get-ADSiteName/1.0/Content/Get-ADSiteName.ps1
Option 5: https://www.reddit.com/r/PowerShell/comments/4cjdk8/get_the_ad_site_name_of_a_computer/
Option 6: https://learn.microsoft.com/en-us/windows/win32/adsi/iadsadsysteminfo-property-methods