Get-AdUser SamAccountName in Active Directory

Get-AdUser SamAccountName attribute is a logon name in the previous version of the Windows system. SamAccountName logon name has a maximum 20 character length limit and a unique name for security principal objects within the domain.Get-AdUser cmdlet in PowerShell gets all of the properties for the aduser along with the samaccountname attribute.

You can get aduser properties using its SAMAccountName (Security Account Manager), email address, display name, userprincipalname.

This article will explain how to get aduser SAMAccountName attribute using the email address, display name, get-aduser using samaccountname, and more with get-aduser examples.

Get-AdUser SamAccountName for all Users

You can get aduser all SAMAccountName attribute from the active directory using the below command

Get-ADUser -Filter * | Select GivenName, SAMAccountName

In the above PowerShell script, Get-AdUser uses the Filter parameter to retrieve all ad users from the active directory and select GivenName and SAMAccountName.

The output of the above command to Get-AdUser SAMAccountName for all users as below

GivenName SamAccountName
--------- --------------

Tom       toms
Erick     ErickJ
Gary      garyw
Chris     chrisd

You can use Export-Csv cmdlet in PowerShell to export GivenName and SamAccountName retrieved in the above command to CSV file as given below

 Get-ADUser -Filter * | Select GivenName, SAMAccountName | Export-Csv -Path C:\PowerShell\samaccountname.csv -NoTypeInformation

This command gets GivenName and SamAccountName and uses Export-CSV to export aduser samaccountname to CSV file.

Get-AdUser by SAMAccountName

You can get aduser by SAMAccountName using the below command

Get-ADUser -Identity ErickJ

In the above command, Get-ADUser cmdlet gets aduser by SAMAccountName using Identity parameter.

Get-AdUser by SAMAccountName
Get-AdUser by SAMAccountName

Cool Tip: Using Get-ADObject to find active directory objects in PowerShell!

Get-AdUser SAMAccountName from Email Address

You can get aduser samaccountname from email address using Get-AdUser filter parameter as given below

 Get-AdUser -Filter {EmailAddress -eq "[email protected]"} | Select-Object -ExpandProperty SAMAccountName

In the above PowerShell script, it gets aduser samaccountname from email address specified in the Filter parameter.

Get AdUser SAMAccountName from CSV

If you have a list of active directory user email address in CSV file and wants to retrieve samaccountname from an email address, run the given below command

Import-Csv -Path C:\PowerShell\AdUserEmailList.csv | 
foreach {

    Get-ADUser -Filter "EmailAddress -eq '$($_.user)'" | Select-Object -ExpandProperty SamAccountName

In the above PowerShell script,

AdUserEmailList.csv file contains ad users email addresses in the below format

"[email protected]"
"[email protected]"

Using Import-Csv cmdlet, it imports specified CSV file. In the next statement, it uses a foreach loop to iterate objects and use it in the Get-AdUser cmdlet.

Get-AdUser cmdlet uses a Filter parameter to check the condition EmailAddress eq to the user email address and get aduser samaccountname.

Get-AdUser SAMAccountName from DisplayName

You can get-aduser samaccountname from display name using Get-AdUser filter parameter as given below

 Get-AdUser -Filter {DisplayName -eq "gary willy"} | Select-Object -ExpandProperty SAMAccountName

In the above PowerShell script, it gets aduser samaccountname from displayname specified in Filter condition where DisplayName -eq “gary willy”

Get-AdUser Filter SamAccountName like

You can get aduser from active directory filter by samaccountname as given below

 Get-AdUser -Filter {SamAccountName -like "tom*"}

In the above PowerShell script, it gets aduser filter samaccountname like tom* and gets aduser properties.

We have used wildcard character * to filter samaccountname starts with tom.

The output of the above script as below

PS C:\Windows\system32> Get-AdUser -Filter {SamAccountName -like "tom*"}

DistinguishedName : CN=Tom Smith,OU=SALES,DC=SHELLPRO,DC=LOCAL
Enabled           : True
GivenName         : Tom
Name              : Tom Smith
ObjectClass       : user
ObjectGUID        : 1f3a2572-2621-4e47-9bdf-81d1f8172f69
SamAccountName    : toms
SID               : S-1-5-21-1326752099-4012446882-462961959-1103
Surname           : Smith
UserPrincipalName : [email protected]


I hope the above article on Get-AdUser SamAccountname is helpful to get samaccountname by email address or get aduser samaccountname by displayname.

Read more about on get-aduser blog posts where I explained to get-aduser by email, get aduser properties, get-aduser filter from specific ou

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.

Leave a Comment