So, I have a single forest with multiple domains. I wanted to use the native ActiveDirectory module for find a SamAccountName.
I came up with this:
$sam = "mysam" $domains = (Get-ADForest).domains ForEach($domain in $domains){ Write-Host $domain Get-ADUser -Filter 'SamAccountName -eq $sam ' -Server $domain -Properties *| select DistinguishedName }
Then I had a brain fart! Why not use a GC? Its quicker ;-)
$sam = "mysam" $forest = (Get-ADForest).Name + ":3268" Get-ADUser -Filter 'SamAccountName -eq $sam' -Server $forest -Properties * | select DistinguishedName
enjoy!