Convert a list of displaynames to samaccountnames using PowerShell.txt - Notepad

Convert a list of displaynames to samaccountnames using PowerShell

The below PowerShell script reads a list of AD User/Computer object displaynames (eg Nicholas Beare) and returns the corresponding samaccountname (eg nbeare).
$users = Get-Content h:\fullnames.txt

foreach ($user in $users)

{

Get-ADUser -ldapfilter "(displayname=$user)" -Property samaccountname | Select-Object -Property samaccountname

}


The below script is another way to do this. It formats the result and extracts it to a text file.
Get-Content h:\fullnames.txt |

ForEach-Object{

get-aduser -ldapfilter "(displayname=$_)" -property samaccountname,displayname | Select-Object -Property samaccountname,displayname

} |

Format-Table -Auto | Out-File h:\result.txt


Back