Read list of sAMAccountName and get DistinguisedName

in #powershell8 years ago

Company LANID or USERID all in a text file with one id on each line to retrieve the users Full Name

script assumes c:\scripts path has a file named usersnames.txt. you can replace this with your own path

$User = $env:USERNAME
$source = Get-Content c:\scripts\usernames.txt
foreach ($User in $source)
{
$Search = New-Object DirectoryServices.DirectorySearcher([ADSI]“”)
$Search.filter = “(&(objectClass=user)(sAMAccountName=$User))”
$results = $Search.Findall()

Foreach($result in $results){
$User = $result.GetDirectoryEntry()
$realname = $user.DistinguishedName

the output of the file can be found in c:\temp\realname.txt

$realname | out-file -append c:\temp\realname.txt
}
}

Coin Marketplace

STEEM 0.17
TRX 0.16
JST 0.029
BTC 76095.48
ETH 2918.89
USDT 1.00
SBD 2.65