nuT707
nuT707

Reputation: 1573

Powershell: import-csv and get-aduser

I trying to find users, storing in 'users.csv' in AD:

Import-Module ActiveDirectory
import-csv "\users.csv" | ForEach-Object {
Get-ADUser -Filter {displayname -eq $_.id}}

But PS says that 'id' property not found

users.csv contents:

id
MacAskill Danny
Cedric Gracia

e.t.c

Upvotes: 0

Views: 13524

Answers (3)

knurmia
knurmia

Reputation: 171

I know this is an old post, but it really saved me a lot of headaches. Couldn't figure out how to find the user with imported values, but just by creating a intermediate variable, I was able to get my script to work. I didn't even need to use any double quotes.

Kris

Upvotes: 0

Matt T
Matt T

Reputation: 11

# Use Import-csv and Get-ADUser together
# Import csv that contains "sn" column and get all AD users where
# sn matches any of the values imported from csv
Import-Csv C:\temp\1.csv | select sn -ExpandProperty sn | foreach { Get-ADUser -Filter 'sn -eq $_' }

Upvotes: 1

vonPryz
vonPryz

Reputation: 24071

Edit the CSV file and enclose the displaynames with double quotes like so,

id
"MacAskill Danny"
"Cedric Gracia"

After that, use an itermediate variable like so,

Import-Csv .\users.csv | % {
    $f = $_.id; # Set the displayname into a temp variable
    get-aduser -filter { displayname -eq $f } # Use the temp variable with filter
}

I don't konw why an intermeidate variable is needed. There is something weird going on with passing and parsing variables with the -filter parameter.

Upvotes: 3

Related Questions