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.15
JST 0.028
BTC 56775.06
ETH 2345.64
USDT 1.00
SBD 2.36