PowerShell Code: Find User in Active Directory Forest

 

PowerShell Code: Find User in Active Directory Forest

There are times when you have a userid, but don’t know where in a multi-domain forest a user is located. Here’s some PowerShell code for locating the user’s domain.
PowerShell code leverages the Active Directory PowerShell module to query a local Global Catalog (GC) server in order to identify the user’s domain.


Param
(
$UserID
)

import-module activedirectory

$LocalSite = (Get-ADDomainController -Discover).Site
$NewTargetGC = Get-ADDomainController -Discover -Service 6 -SiteName $LocalSite
IF (!$NewTargetGC)
{ $NewTargetGC = Get-ADDomainController -Discover -Service 6 -NextClosestSite }
$NewTargetGCHostName = $NewTargetGC.HostName
$LocalGC = “$NewTargetGCHostName” + “:3268”

Write-Output “Identify User and Computer Objects with configured Service Principal Names `r ”
Get-ADUser $UserID -Server “$LocalGC”

Get-ADUser -Server $LocalGC -filter { sAMAccountName -eq $UserID }

(Visited 6,594 times, 1 visits today)