I'm sure this is just a syntax error, but I'm trying to search AD users and I cannot figure out why this does not work:
Write-Host "Enter LastName or UserName:"
$x = Read-Host
Get-ADUser -Filter { SAMAccountName -like '*$x*' } -Properties DisplayName | FT -Properties DisplayName
Just doesn't return anything. I'm sure it is syntax with the "*", but I'm not sure why.
$x is not expanded inside the Filter scriptblock, this should do the job:
$x = 'mini'
Get-ADUser -Filter "SamAccountName -like '*$x*'" -Properties DisplayName | ft DisplayName
DisplayName
-----------
Administrator
Alternatively, you could use ldap filter:
Get-ADUser -LDAPFilter "(samaccountname=*$x*)"