Import-csv | get-ADUser (path not valid)

I have this code:

Import-CSV -Path 'C:\Users\me\Desktop\All_EXO.csv' | ForEach-Object { 
Get-ADUser -Filter {UserPrincipalName -eq $($_.UserPrincipalName)} -Properties GivenName,sn,mail,department,AccountType,Team,JobCode| select GivenName,sn,mail,department,AccountType,Team,JobCode
}

…but it’s balking about an invalid Path, yet, I can see the contents of the csv (has a header, ‘UserPrincipalName’)

Could you please share the complete error message?

Get-ADUser : Cannot process argument because the value of argument "path" is not valid. Change the value of the "path" argument and run the operation again.

Try it this way:

Import-CSV -Path 'C:\Users\me\Desktop\All_EXO.csv' | 
ForEach-Object {
    $upn = $_.UserPrincipalName
    Get-ADUser -Filter "UserPrincipalName -eq '$upn'" -Properties GivenName,sn,mail,department,AccountType,Team,JobCode | 
        Select-Object -Property GivenName,sn,mail,department,AccountType,Team,JobCode
    }
1 Like

Thanks Olaf…Can you share briefly how you approached this solution for my edification?

I pasted the error message you posted into the search box of Google and clicked the first hit. :wink:

Which was this:

ah, “they need to be properly wrapped with quotes.”

Yeah I google too (a lot) but I doubt I woulda found it before Powershell.org!

thanks again

The type of the parameter -Filter is [string]. Even though PowerShell in general and the cmdlet Get-AdUser in particular are actually very forgiving you cannot use a scriptblock with variable substitution for it. The variable has to be expanded before. :wink:

1 Like