I am trying to get only active AD Users in AD which satisfies below conditions:
- samaccountname is not null
- mail is not null
- mail ends with a domain, in this case what is contained in @oldDomain variable
As a demo, I have also filter by only a samaccountname. Below is not working:
$Users = Get-ADUser -Filter {(samaccountname -ne "null") -and (samaccountname -eq "TestUserPruebasSI") -and (Enabled -eq "true") -and (mail -ne "null") -and "mail -like '*$oldDomain'"}
The culprit is the last criteria "mail -like '*$oldDomain'". If I remove it then query Get-ADUser works.
So how can I solve this?
"mail -like '*$oldDomain'"->mail -like "*$oldDomain"