Get users SAMaccount name from Full name

active-directory, powershell

Solution

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

Problem

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 } ```

Original source