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
LakelandBankAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPOwnerCommented:
$today = (get-date).ToString("MM/dd/yyyy")
#note the space after "AFTER"
$todelete = "DELETE AFTER " + $today
Get-ADUser -SearchBase "OU=MyOU,DC=MyDomain,DC=COM -Filter {Description -like  $todelete -Properties name, DistinguishedName, Description | select name, DistinguishedName, Description | sort Description 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
$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).
0
LakelandBankAuthor Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.