Filter is finicky

Get-ADUser -Filter is really finicky. I finally figured out what I was doing wrong with the help of this wonderfully detailed Powershellish.com post.

Basically, I can’t use properties of variables with -Filter

This does NOT work:

foreach ($user in $userlist) {
  $ADUser = Get-ADUser -Filter{(Surname -eq $user.Surname) -and (GivenName -eq $user.GivenName)}
}

This does work:

foreach ($user in $userlist) {
  $ln = $user.Surname
  $fn = $user.GivenName
  $ADUser = Get-ADUser -Filter{(Surname -eq $ln) -and (GivenName -eq $fn)}
}
Advertisements