Extract CSV data for query Get-ADUser

Hello,

Sorry for my english, i’m French.

Hello, I have a CSV file with a column without a header that contains numbers.

I would like to use them in a Get-ADUser request. I have tried several things but without success. Here is the last code tested:

Import file CSV with adding header Numero
$Users = Import-Csv -Path c:\test\compare.csv -Header 'Numero'

Clear the contents of the Result.csv output file which may contain old data
clear-content C:\test\powershell\result.csv

Query on the AD to find the name and the first name of the users who are present in the file Numero.csv
$List = ($Users | ForEach-Object {                      
               Get-ADUser -Filter 'attribute -Like "$_.Numero"' -Properties * | select-object givenname,sn,attribute
               })

Export du contenu de $List dans le fichier final Result.csv
$List | Export-CSV -Path "C:\test\powershell\Result.csv" -NoTypeInformation -Encoding UTF8 -Delimiter ";"

Thanks a lot

You have duplicate posts, but your issue is most likely here:

Get-ADUser -Filter 'attribute -Like "$_.Numero"' -Properties *

Single qoutes are literal, nothing will be expanded

PS C:\Users\rasim> 
$numero = 123
'attribute -Like "$Numero"'

attribute -Like "$Numero" #with single qoute, you are literally searching for this

PS C:\Users\rasim> 
$numero = 123
"attribute -eq $Numero" 

attribute -eq 123 #the double qoutes will expand the filter. This should work without qoutes.

PS C:\Users\rasim> 
$numero = 123
"attribute -eq '$Numero'"

attribute -eq '123' #if the above format doesn't work, try this

Switch your single/double quotes and use a subexpression $(…)

$List = $Users | ForEach-Object {                      
    Get-ADUser -Filter "attribute -Like '$($_.Numero)'" -Properties * | select-object givenname,sn,attribute
}

Or use a named variable instead of automatic variable

$List = foreach($user in $users){
    Get-ADUser -Filter "attribute -Like '$user'" -Properties * | select-object givenname,sn,attribute
}

Thanks

I use a named variable but the returned value is not the right one

For example

if i do :

$List = foreach($user in $users){                                
                write-host $user
}

I have for results

@{Numero=10620}

 

The result I want to display is just :

10620

i have tried several things but it doesn’t work.
Do I have to create an object at the start of the script?

Thanks a lot

You have at least 3 different options:

$List = 
foreach ($user in $users.Numero) {                                
    $user
}
$List

$List = 
foreach ($user in $users) {                                
    $user.Numero
}
$List

$List = 
foreach ($user in $users) {                                
    $user
}
$List.Numero

 

Thanks, i get i

here is the final script

clear-content C:\test\powershell\comparewithname.csv

$Users=Import-Csv -Path c:\test\Compare.csv  -Header 'Numero'|
 ForEach-Object{                        
               Get-ADUser -Filter "attribute -eq '$($_.Numero)'" -Properties *| select-object givenname,sn,attribute
               }| Export-CSV -Path "C:\test\powershell\CompareWithName.csv" -NoTypeInformation -Encoding UTF8 -Delimiter ";"

Thank you to all of you

Just as a tip: To reduce the stress you put on your AD running your query you should only query attributes with the -Properties parameter you’re going to use afterwards. GivenName and Surname already belong to the property set Get-ADUser outputs by default. So that the following should be enough then …

Import-Csv -Path c:\test\Compare.csv  -Header 'Numero' |
ForEach-Object {
    Get-ADUser -Filter "attribute -eq '$($_.Numero)'" -Properties attribute | 
    Select-Object -Property GivenName, Surname, attribute
} | 
Export-CSV -Path 'C:\test\powershell\CompareWithName.csv' -NoTypeInformation -Encoding UTF8 -Delimiter ';'