Read list of sAMAccountName and get DistinguisedName

in powershell •  8 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
}
}

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!