New to PowerShell! I am not seeing any output from the below foreach query. It appears to run but there is no output to the console or redirecting output to a file. I structured the filter syntax as per a Q&A found here on powershell.org “Trouble with Get-Aduser from CSV” but the recommendation does not work for me. What I am I doing wrong?
Partial CSV File:
Name
0915_jc
0923_ja
1008_ha
1071_bd
1169_hm
1173_km
1599_my
1810_ih
Code:
$dirs=Import-Csv .\user-dirs.csv
foreach ($line in $dirs) {
Get-ADUser -Filter “homedirectory -like ‘$($line.name)’” -Properties * |format-table name, enabled, lastlogondate -Autosize
}
Code works, are you sure the homedirectory attribute has a value?
The code is also working for me.
You might want to try something like:
import-csv $path get-aduser -filter {homedirectory -like $_.name }Properties homedirectory,name, enabled,lastlogondate | format-table -autosize
You don’t need to do for each as each entry will go through the pipe line.
In line 3 of your code you have “$line.name”. You try to access the name property of the object that the $line variable holds. That property does not exist. Therefor no output.
$line = "0915_jc"
$line.name
$line | get-member
Try to keep it simple. Delete to lines from the csv (Name + ____) and use txt.
See if this works for you (I didn’t test it)
$dirs=get-content .\user-dirs.txt
foreach ($dir in $dirs) {
Get-ADUser -Filter "homedirectory -like '*$dir*'" -Properties name,enabled,lastlogondate
}
Thank you all for the responses and suggestions! Seeing that this worked for other people, I looked outside of the command structure and focussed on my CSV file. In the file, the headings had a row beneath them that were populated with dashes (underlining). I removed that row and it all worked fine. Pretty much garbage in, garbage out.
Name
_____ <— This row was the cause of my problem
0915_jc
…