Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get users SAMaccount name from Full name

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?

$users = Get-Content C:\users\admin\Desktop\move.txt

foreach ($user in $users){
Get-ADUser -Filter {Name -eq "$user"} |Select-Object name, samaccountname
}
like image 402
JoeRod Avatar asked Nov 25 '13 15:11

JoeRod


1 Answers

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):

Get-ADUser -Filter "Name -eq '$user'" | Select-Object name, samaccountname
like image 74
Shay Levy Avatar answered Sep 28 '22 06:09

Shay Levy