I need some guidance from the Powershell gods as I’ve been scratching my head over this for ages now and have far too many browser tabs open.
I’m trying to retrieve a list of new start accounts of people starting within the next 30 days. In my org we set the users Start Date in a custom attribute, lets call this attribute: custAtt1. The data stored in this attribute is in the format of dd/mm/yyyy
The issue that I’m facing is that my results are returning dates outwith my date range (I’m pulling in results beyond my 30 day limit). I came across Adam Bertrams blog about having to potentially ‘casting’ strings into the DateTime format but I’m not sure how I can do this across multiple records. Ref: The Practical PowerShell Get Date to Demystify Date and Time
Here is my code:
$NewStarts = Get-ADUser -SearchBase "OU=<“Removed>” -Filter {(custAtt1 -lt $30Days)} -Properties Name,custAtt1
Write-Host "There are"$NewStarts.count" people due to start in the next 30 days"
$NewStarts | ft Name,custAtt1```
I assume you defined the varialbe $30Days before … anyway …
-Filter {(custAtt1 -lt $30Days)}
Your custAtt1 seems to be of the type [String] and comparing a String to a DateTime will not provide reasonable results.
So I’m afraid that you will have to collect all users and filter them with a Where-Object where you - first - turn your custAtt1 into a proper DateTime value and - second - compare it against your $30Days variable.
Well, I don’t know if that counts as cleaner but you could skip the part with the export to a file and reading it again. That should speed up the snippet noticeably.