Question:
I have a list of users full name that I’d like to get their SAMaccount name but when I run my code I get no results. Anyone have any ideas?
1 2 3 4 5 6 |
$users = Get-Content C:\users\admin\Desktop\move.txt foreach ($user in $users){ Get-ADUser -Filter {Name -eq "$user"} |Select-Object name, samaccountname } |
Answer:
It could be that $user is null inside the script block. Try to use double quotes instead of braces and put the variable in single quotes to make the valid query (name contain spaces):
1 2 |
Get-ADUser -Filter "Name -eq '$user'" | Select-Object name, samaccountname |