Home » PowerShell » Get-AdUser Employee ID in PowerShell

Get-AdUser Employee ID in PowerShell

As Administrators, often we need to get user employee id or employee number from the active directory or get aduser samaccountname from employee id to get more information about active directory users.

In this article, I will explain how to get aduser employee id in the active directory using PowerShell script and how to get aduser samaccountname from employee id.

To get active directory user information, we will use PowerShell Get-AdUser cmdlet to get one or more ad users in the active directory based on search criteria.

Let’s understand get-aduser employee id in active directory using PowerShell.

Get-AdUser EmployeeId in Active Directory

You can use Get-AdUser cmdlet to get active directory employee id with filter wildcard character to search within the domain and get aduser EmployeeId property as below

Get-ADUser -Filter "*" -Property EmployeeID | Select Name,EmployeeId

In the above PowerShell script, Get-AdUser cmdlet in active directory get aduser from domain and select aduser employee id, name and display on console as below

PS C:\Windows\system32> Get-ADUser -Filter "*" -Property EmployeeID | Select Name,EmployeeId

Name        EmployeeId
----        ----------
Guest
krbtgt
Tom Smith   1
Erick Jones 3
Gary Willy  4
Chris Dore  2
adam        6
nathan      8
Don         7


PS C:\Windows\system32>

Cool Tip: How to add user accounts to AD Groups in PowerShell!

Get AdUser by EmployeeId

You can get active directory user by employeeid using Get-AdUser as given below

Get-ADUser -Filter "EmployeeID -eq 1" -Properties *

In the above PowerShell script, get aduser filter employeeid equal to 1 gets an active directory user by employee id.

Get AdUser SamaccountName from Employee Id

If you want to get aduser samaccountname from employee id, use Get-AdUser cmdlet with filter parameter where employee id equal to provided employee id

Get-ADUser -Filter "EmployeeID -eq 1" -Properties SAMAccountName

In the above command, it filters employee id equal to 1 and gets aduser samaccountname and user information.

The output of above get-aduser samaccountname from employee id script as below

Get-AdUser Samaccountname from Employee Id
Get-AdUser Samaccountname from Employee Id

If you want to get aduser samaccountname information by employeeid, you can get it using Get-AdUser cmdlet in PowerShell.

$Users = Get-ADUser -Filter "*" -Property EmployeeID | Where { $_.EmployeeId -ne $null }  | Select EmployeeId
Foreach ($user in $Users)
{
   $EmployeeId = $user.EmployeeId
   Get-ADUser -Filter {EmployeeId -eq  $EmployeeId} -Properties * | Select SamAccountName
    
}

In the above PowerShell script,

The first command uses the Get-ADUser cmdlet to get an aduser where Employeeid not null and select EmployeeId in $Users.

In the next statement, iterate over $Users using Foreach and get adusers samaccountname by employeeid

Cool Tip: How to enable adaccount in PowerShell!

Get AdUser EmployeeNumber

Active Directory user has EmployeeNumber and employee id attributes. You can get aduser employeenumber using the Get-AdUser cmdlet as below

Get-ADUser -Identity Toms -Properties * | Select EmployeeNumber,EmployeeId

Above PowerShell script, get aduser EmployeeNumber for the user Toms Specified by Identity parameter. Use Properties * to select EmployeeId and EmployeeNumber.

Conclusion

I hope the above article to get-aduser employee id in active directory and get-aduser samaccountname from employee id using PowerShell helpful to you.

Using the Get-AdUser in PowerShell, you can get aduser employeeid and employeenumber attributes from the active directory.

Read more to export ad user to csv file in PowerShell!

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