# List accounts within a certain date

**URL:** <https://forums.powershell.org/t/list-accounts-within-a-certain-date/17648>\
**Category:** PowerShell Help\
**Created:** [September 27, 2021, 4:25pm UTC](https://forums.powershell.org/t/list-accounts-within-a-certain-date/17648 "2021-09-27T16:25:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![simcot](https://avatars.discourse-cdn.com/v4/letter/s/7ea924/32.png) [@simcot](https://forums.powershell.org/u/simcot)\
**Post date:** [September 27, 2021, 4:25pm UTC](https://forums.powershell.org/t/list-accounts-within-a-certain-date/17648/1 "2021-09-27T16:25:17Z")

</div>

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](https://adamtheautomator.com/powershell-get-date/)

Here is my code:

````auto
$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```
````

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [September 27, 2021, 6:44pm UTC](https://forums.powershell.org/t/list-accounts-within-a-certain-date/17648/2 "2021-09-27T18:44:34Z")

</div>

I assume you defined the varialbe `$30Days` before … anyway …

```auto
-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.

---

<div class="post-metadata">

**Author:** ![simcot](https://avatars.discourse-cdn.com/v4/letter/s/7ea924/32.png) [@simcot](https://forums.powershell.org/u/simcot)\
**Post date:** [September 27, 2021, 7:06pm UTC](https://forums.powershell.org/t/list-accounts-within-a-certain-date/17648/3 "2021-09-27T19:06:30Z")

</div>

Yes sorry, I have defined that variable:

`$30Days = ((Get-Date).AddDays(30)).ToString("dd/MM/yyyy")`

I’ll take on your suggestion and report back, thanks!

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [September 27, 2021, 7:14pm UTC](https://forums.powershell.org/t/list-accounts-within-a-certain-date/17648/4 "2021-09-27T19:14:55Z")

</div>

Another tip in advance: When you define your variable to compare against do not turn it into a string type - remove the `.ToString("dd/MM/yyyy")`.

In PowerShell we work with objects and properties - not with strings. 😉

---

<div class="post-metadata">

**Author:** ![simcot](https://avatars.discourse-cdn.com/v4/letter/s/7ea924/32.png) [@simcot](https://forums.powershell.org/u/simcot)\
**Post date:** [October 1, 2021, 1:12pm UTC](https://forums.powershell.org/t/list-accounts-within-a-certain-date/17648/5 "2021-10-01T13:12:58Z")

</div>

Fixed it!

I collected all of my users and then ran a for-each object to convert the string to DateTime:

```auto
$30Days = (Get-Date).AddDays(30)
Get-ADUser -SearchBase "OU=<Removed>" -Filter * -Properties Name,custAtt1 | Export-Csv -NoTypeInformation -Path $env:TEMP\NewStarts.csv
$NewStarts = Import-Csv $env:TEMP\NewStarts.csv
$NewStarts | ForEach-Object {
    $_.custAtt1 = [datetime]::parseexact($_.custAtt1,'dd/MM/yyyy',$null) 
    }

$NewStartWithin30Days = ($NewStarts | Where-Object {$_.custAtt1 -lt $30Days}) 

```

I’m sure there is a cleaner way to do this but I’m happy that the data being returned is correct so hopefully this may help someone else out there 🙂

Thanks for your help Olaf.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [October 4, 2021, 9:58pm UTC](https://forums.powershell.org/t/list-accounts-within-a-certain-date/17648/6 "2021-10-04T21:58:46Z")

</div>

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. 😉

```auto
$30Days = (Get-Date).AddDays(30)
$NewStartWithin30Days = 
Get-ADUser -SearchBase "OU=<Removed>" -Filter * -Properties Name, custAtt1 | 
    Select-Object -Property *,@{Name = 'CustAttr1Parsed'; Expression = {[datetime]::parseexact($_.custAtt1, 'dd/MM/yyyy', $null)}} |
        Where-Object { $_.CustAttr1Parsed -lt $30Days }

```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:21pm UTC](https://forums.powershell.org/t/list-accounts-within-a-certain-date/17648/7 "2024-05-16T20:21:15Z")

</div>


