To get SAMAccountName from distinguishedName in the Active Directory for the given user, use the Get-AdUser filter to check aduser distinguishedName and return samaccountname and other active directory user attributes.
The Get-AdUser cmdlet in the PowerShell gets a specified user object or performs a search to retrieve multiple user objects from the active directory.
In this article, we will discuss in PowerShell how to get SAMAcccountName from the distinguishedname.
Get SAMAccountName from the DistinguishedName in PowerShell
If you have distinguishedName for the active directory user and want to get SAMAccountName for the given aduser in the Active Directory, run the following command.
$user = 'CN=Netya Xu,OU=FINANCE,OU=SHELLUSERS,DC=SHELLPRO,DC=LOCAL' Get-AdUser -Filter {distinguishedName -eq $user} | Select SAMAccountName,DistinguishedName
In the above PowerShell script, the $user variable stores the ad user distinguished name.
Use the Get-AdUser Filter command to perform a search for the aduser object where distinguishedName is equal to specified distinguishedName.
If the results match, it will pass the aduser object to the Select command to display SAMAccountName and DistinguishedName for the ad user.
The output of the above PowerShell script retrieves SAMAccountName using the DistinguishedName and displays it on the terminal.
Cool Tip: How to use the Get-AdUser cmdlet with examples!
PowerShell Find SAMAccountName from the list of DistinguishedName
If you have distinguishedName stores in the CSV file and the requirement is to get SAMAccountName for the user from the list of distinguishedName, we will use the Get-AdUser cmdlet in the PowerShell to get aduser properties.
Let’s consider the list of distinguishedName for the users in the CSV file stores as below:
"DistinguishedName"
"CN=Guest,CN=Users,DC=SHELLPRO,DC=LOCAL"
"CN=krbtgt,CN=Users,DC=SHELLPRO,DC=LOCAL"
"CN=Tom Smith,OU=SALES,DC=SHELLPRO,DC=LOCAL"
"CN=Erick Jones,OU=HR,DC=SHELLPRO,DC=LOCAL"
"CN=Gary Willy,OU=HR,DC=SHELLPRO,DC=LOCAL"
To get the SAMAccountName from the distinguishedName list of ad users stored in the CSV file, we will first need to import it and iterate using the foreach loop.
Import-Csv -path "C:\PowerShell\AdUser-DistinguishedName.csv" | Foreach {Get-ADUser -Filter "distinguishedName -eq '$($_.DistinguishedName)'" | Select SamAccountName,DistinguishedName}
In the above PowerShell script, the Import-CSV cmdlet in the PowerShell is used to import the CSV file having the list of distinguishedName and passed it as input to the Foreach loop.
The Foreach loop iterate over the list of distinguishedName available in the CSV file.
The Get-AdUser uses the Filter parameter to check if the distinguishedName is equal to the name from the CSV file. If the result matches, it displays the SAMAccountName for the given aduser.
The output of the above PowerShell script to retrieve the SAMAccountName from the list of the distinguishedName is:
Cool Tip: How to get aduser SAMAccountName in the Active Directory!
Conclusion
I hope the above article to get SAMAccountName from distinguishedName in PowerShell is helpful to you.
Use the Get-AdUser Filter parameter to search for the ad user objects in the Active Directory.
You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.