Ask Your Question
1

How can I use Powershell to retrieve Active Directory user information based on their 'departmentnumber' that matches a list of department numbers provided in a CSV file?

asked 2022-11-08 11:00:00 +0000

pufferfish gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-03-04 16:00:00 +0000

lalupa gravatar image

Here is a PowerShell script that retrieves Active Directory user information based on their department number that matches a list of department numbers provided in a CSV file:

# Load the list of department numbers from the CSV file
$departments = Import-Csv -Path "C:\Temp\Departments.csv"

# Loop through each department number in the list and retrieve user information
foreach ($department in $departments) {
    # Retrieve users based on department number
    $users = Get-ADUser -Filter "DepartmentNumber -eq '$($department.DepartmentNumber)'" -Properties *

    # Display user information
    $users | Select-Object Name, SamAccountName, Title, Department, OfficePhone
}

In this script, we first load the list of department numbers from the CSV file using the Import-Csv cmdlet. We then loop through each department number in the list and retrieve user information using the Get-ADUser cmdlet. We pass a filter to the cmdlet to only retrieve users whose DepartmentNumber property matches the current department number in the loop.

We also specify the -Properties * parameter to retrieve all properties of the user object. Finally, we select and display the user information we are interested in using the Select-Object cmdlet.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-11-08 11:00:00 +0000

Seen: 8 times

Last updated: Mar 04 '23