0

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?

1
  • "mail -like '*$oldDomain'" -> mail -like "*$oldDomain" Commented Apr 26, 2019 at 9:50

1 Answer 1

1

Using below works:

$Users = Get-ADUser -Filter "samaccountname -ne 'null' -and samaccountname -eq 'TestUserPruebasSI' -and Enabled -eq 'true' -and mail -ne 'null' -and mail -like '*$oldDomain'"
Sign up to request clarification or add additional context in comments.

1 Comment

You can safely skip the samaccountname -ne 'null' and mail -ne 'null' parts, because later on you filter on these properties to have a value to compare against. Besides.. is it possible for a user to NOT have a SamAccountName ??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.