
Today one of my IT coworkers, in another department, sent a couple of emails to the Ops to get the username (SamAccount) from a couple of Active Directory users accounts. This guy, which is not familiar with AD, had only the DisplayName properties information.
I wrote him back that he could just request RSAT(Remote Server Administration Tools) to be installed on his workstation or just use this small PowerShell that I just wrote in minutes. Since Active Directory does not require any specific permission to access this kind of information. Here is the code, nothing advanced, but it does the work ;-)
function Get-DomainUser {
PARAM($DisplayName)
$Search =
[adsisearcher]"(&(objectCategory=person)(objectClass=User)(displayname=$DisplayName))"
foreach ($user in $($Search.FindAll())){
New-Object -TypeName PSObject
-Property @{
"DisplayName" = $user.properties.displayname
"UserName" = $user.properties.samaccountname
"Description" = $user.properties.description}
}
}
Result
PS C:\> Get-DomainUser -DisplayName "jonathan*" | Format-List
UserName : {JonathanD}
Description : {Account of Jonathan Delpiero}
DisplayName : {Jonathan Delpiero}
UserName : {DumoulinJ}
Description : {Account of Jonathan Dumoulin}
DisplayName : {Jonathan Dumoulin}