I’ve tried all kinds of things to get the user’s displayName from AD inside of PowerShell 2.0 but can’t seem to find something that works. The majority of computers will not have the Active Directory module installed and there are still some PowerShell versions below 5.1 with the lowest still present being 2.0.
I’ve tried
$adsysinfo = New-Object -ComObject "ADSystemInfo"
$adsysinfo.GetType().InvokeMember("$($env:USERNAME)",$Null,$adsysinfo,$null)
Which gives me
+ $adsysinfo.GetType().InvokeMember <<<< ("$($env:USERNAME)",$Null,$adsysinfo,$null)
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
Also tried
$Searcher = New-Object DirectoryServices.DirectorySearcher
$Searcher.SearchRoot = 'LDAP://DC=DOMAIN,DC=DOMAIN'
$Searcher.Filter = '(&(objectCategory=person))'
$Searcher.FindAll("$env:USERNAME")
#Note: I also tried
$Searcher.FindOne("$env:USERNAME")
Which gives me
Cannot find an overload for "FindAll" and the argument count: "1".
At line:1 char:18
+ $Searcher.FindAll <<<< ("$env:USERNAME")
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
#AND
Cannot find an overload for "FindOne" and the argument count: "1".
At line:1 char:18
+ $Searcher.FindOne <<<< ("$env:USERNAME")
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
Any suggestions on how I can get the AD user’s displayName in this environment?