Home » PowerShell » Get-AdUser SamAccountName in Active Directory

Get-AdUser SamAccountName in Active Directory

Get-AdUser SamAccountName attribute is a user 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, and 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 SAMAccountName attribute from the active directory using the following script.

Get-ADUser -Filter * | Select GivenName, SAMAccountName

In the above PowerShell script, the 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 the Export-Csv cmdlet in PowerShell to export GivenName and SamAccountName retrieved in the above command to the 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 a CSV file.

Get-AdUser by SAMAccountName

You can get aduser by SAMAccountName using the below command

Get-ADUser -Identity ErickJ

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

Get-AdUser by SAMAccountName
Get-AdUser by SAMAccountName

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

Get-AdUser SAMAccountName from Email Address

You can get aduser samaccountname from the email address using the 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 samaccountname from email address specified in the Filter parameter.

The output of the above PowerShell script to get samaccountname from email address is:

PS C:\> Get-AdUser -Filter {EmailAddress -eq "[email protected]"} | Select-Object -ExpandProperty SAMAccountName

toms

PS C:\>

Get AdUser SAMAccountName from CSV

If you have a list of active directory user email address in a 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

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

Using the 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. It retrieves the list of user logon names.

Get-AdUser SAMAccountName from DisplayName

You can get-aduser samaccountname from display name using the 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" and passes the output to the Select-Object cmdlet to get samaccountname.

The output of the above PowerShell script to get samaccountname from displayname is:

PS C:\> Get-AdUser -Filter {DisplayName -eq "gary willy"} | Select-Object -ExpandProperty SAMAccountName

garyw
PS C:\>

Get-AdUser Filter SamAccountName like

You can get aduser from the 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]

Conclusion

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.