Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.