Reputation: 3
I've got a situation where I need to take a list of emails in a .csv, import the list and pull the applicable properties from Active Directory (via GetAD-User?).
Below is my estimated process flow:
Here is a sample list for testing purposes (Saved As: csv_file.csv):
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
Here is my code:
$Name = @()
Get-Content -path "C:\Location\Of\Sample\Input\csv_file.csv" |
ForEach-Object {
Get-ADuser -filter * -properties
Mail, DisplayName, Department, Office | Select DisplayName, Department,
Office
}
When I run this, I receive a list of ALL AD Users, for each email line listed in my sample csv_file.csv.
My Issue: 1. How do I only show the users whose Mail property is listed in my csv file? For example, I have 6 emails listed, I should show the properties associated with only those 6 addresses. 2. How do I compare the value listed in the .csv to the .Mail property within Get-ADUser?
Example Output: Over 60k+ DisplayName, Department, Office records!
I seem to have difficulty understanding the looping constraints and filters within powershell. I'm certain the root of my issue is -filter *, but what should it be? How do I iterate through each address appropriately? I've referenced a few other posts but cannot seem to identify anything specific to this situation. Constructive criticism is greatly appreciated. Thank you for your help!
Upvotes: 0
Views: 2038
Reputation: 41
This should give you the results you're seeking. One thing to note is that I've used 'EmailAddress' instead of 'Mail' and then filtered on the current EmailAddress.
Get-Content -path "C:\Location\Of\Sample\Input\csv_file.csv" |
ForEach-Object {
Get-ADUser -Filter 'EmailAddress -eq $emailAddress' -Properties Department, DisplayName, EmailAddress | Select-Object DisplayName, Department, Office
}
Upvotes: 1
Reputation: 68341
If you're running Exchange, this will be much quicker:
Get-Content -path "C:\Location\Of\Sample\Input\csv_file.csv" |
Get-Recipient |
Select DisplayName,Department,Office
AD does not use email address as an indexed identity reference, so it has to search the entire directory for every address it cycles through. Exchange does, and maintains it's own user database with email address as an index, so it can ingest the email address directly, without needing to filter. The Get-Recipient
cmdlet returns objects that contain the properties you're looking for.
Upvotes: 1