Home ยป PowerShell ยป Get AdUser from List in PowerShell

Get AdUser from List in PowerShell

Active Directory is a central repository that stores information about users, computers, and other objects in the Windows network. Often administrators need to retrieve the active directory user accounts from the predefined list.

Use the Get-AdUser cmdlet in PowerShell to fetch the AD users based on the provided list.

The `Get-AdUser` command in PowerShell is used to get information about users in the Active Directory. This command retrieves the user accounts from the list containing the user SamAccountName.

In this article, we will discuss how to use the Get-AdUser cmdlet in PowerShell to get a list of users in the Active Directory from the list.

Retrieve Ad User From the List

The Get-AdUser cmdlet in PowerShell queries user accounts in the Active Directory. To fetch the ad user information from the list, follow the given below steps.

  1. Prepare the list โ€“ Create a text file containing a list of user names, with one user name per line. Save the file as .csv file. This file will be used as input for the PowerShell script.
  2. Launch PowerShell Terminal โ€“ Open the PowerShell with administrative privileges.
  3. Run the PowerShell script โ€“ Use the following PowerShell script to get the aduser from the list.
# Read the content of the file
$userNameList = Get-Content -Path C:\PowerShell\adusers_names.csv

# Iterate over the usernames one by one and get aduser information
foreach($username in $userNameList) {
    Get-AdUser -Identity $username | Select Givenname, SamAccountName, UserPrincipalName
}

In the above PowerShell script, the Get-Content cmdlet reads the content of csv file specified in the Path parameter and stores the result in the $userNameList variable.

The Foreach loop iterates over the list of usernames one by one and uses the Get-AdUser command to retrieve information about the specific user.

The Get-AdUser command uses the Identity parameter to specify the username and fetch the user account information. It pipes the result to the Select command that selects the Givenname, SamAccountName, and UserPrincipalName properties of the user.

The output of the above PowerShell script retrieves the ad user information from the list.

Fetch AdUser from list
Fetch AdUser from the list

Cool Tip: How to export ad user to csv in PowerShell!

Conclusion

I hope the above article on how to get aduser from the list using the Get-AdUser cmdlet in PowerShell is helpful to you.

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