LakelandBank
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).ToSt ring("MM/d d/yyyy")
Get-ADUser -SearchBase "OU=MyOU,DC=MyDomain,DC=CO M -Filter {Description -like "DELETE AFTER +$today"} -Properties name, DistinguishedName, Description | select name, DistinguishedName, Description | sort Description
The user description would be "DELETE AFTER MM/DD/YYYY"
Here's my code:
$today = (get-date).AddDays(0).ToSt
Get-ADUser -SearchBase "OU=MyOU,DC=MyDomain,DC=CO
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
Qlemo, I appreciate your input, but that solution didn't work. I got the same results.
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).