Link to home
Start Free TrialLog in
Avatar of LakelandBank
LakelandBankFlag for United States of America

asked on

Powershell: Variable in GET-ADUser Filter not returning objects

I'm trying to query user descriptions to see which users are marked for deletion after a certain date. When I put a variable in the Filter, I'm not getting any results. If remove the variable or put an actual date in the same format as the get-date variable (MM/DD/YYYY), I will get the expected results. Also I have made sure that the "$today" variable matches the dates in the user descriptions.

The user description would be "DELETE AFTER MM/DD/YYYY"

Here's my code:
 $today = (get-date).AddDays(0).ToString("MM/dd/yyyy")
Get-ADUser -SearchBase "OU=MyOU,DC=MyDomain,DC=COM -Filter {Description -like  "DELETE AFTER +$today"} -Properties name, DistinguishedName, Description | select name, DistinguishedName, Description | sort Description
ASKER CERTIFIED SOLUTION
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
$today = (get-date).ToString("MM/dd/yyyy")
Get-ADUser -SearchBase "OU=MyOU,DC=MyDomain,DC=COM -Filter {Description -like  "DELETE AFTER $today"} -Properties name, DistinguishedName, Description |
  select name, DistinguishedName, Description |
  sort Description

Open in new window

should work, too. But the filter expression has some special behaviour in regard of vars and expressions, so you never can tell exactly. Simple var substitution should not be an issue though (opposite from using object properties).
Avatar of LakelandBank

ASKER

Thank you David, that worked perfectly. I appreciate your assistance.

Qlemo, I appreciate your input, but that solution didn't work. I got the same results.