# Select objects in folder by date range

**URL:** <https://forums.powershell.org/t/select-objects-in-folder-by-date-range/5332>\
**Category:** PowerShell Help\
**Created:** [December 9, 2015, 1:18am UTC](https://forums.powershell.org/t/select-objects-in-folder-by-date-range/5332 "2015-12-09T01:18:36Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![steve-jackson](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/steve-jackson/32/228_2.png) [@steve-jackson](https://forums.powershell.org/u/steve-jackson)\
**Post date:** [December 9, 2015, 1:18am UTC](https://forums.powershell.org/t/select-objects-in-folder-by-date-range/5332/1 "2015-12-09T01:18:36Z")

</div>

I am trying to select objects in a folder by date range

`Function Get-DateRange
{
[System.Reflection.Assembly]::LoadWithPartialName(“System.windows.forms”) |
out-null`

`
$WinForm = New-Object Windows.Forms.Form
$WinForm.text = “Calendar Control”
$WinForm.Size = New-Object Drawing.Size(411,190)
$Calendar = New-Object System.Windows.Forms.MonthCalendar
$Calendar.MaxSelectionCount = 356
$Calendar.SetCalendarDimensions([int]2,[int]1)
$WinForm.Controls.Add($Calendar)
$WinForm.Add_Shown($WinForm.Activate())
$WinForm.showdialog() | Out-Null
$Calendar.SelectionRange
} #end function Get-DateRange
***Entry point to script***
$dates = Get-DateRange
`

`Get-ChildItem -path “\server.domain.com\path” | Where-Object { $.LastWriteTime -like ($dates).start -and $.LastWriteTime -like ($dates).end}`

When I run this the calender comes up and when I select my date range and then close but the result of the files to not populate.. I do not get any errors it just returns nothing. so I think what I am doing wrong is just in the last bit.

Can someone help me sort this out?

---

<div class="post-metadata">

**Author:** ![curtis-smith](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/curtis-smith/32/121_2.png) [@curtis-smith](https://forums.powershell.org/u/curtis-smith)\
**Post date:** [December 9, 2015, 1:38am UTC](https://forums.powershell.org/t/select-objects-in-folder-by-date-range/5332/2 "2015-12-09T01:38:58Z")

</div>

You problem is here with your Where-Object. -like is not the correct operator to use. You need -gt and -lt

```
Get-ChildItem -path "\\server.domain.com\path" | Where-Object { $_.LastWriteTime -gt ($dates).start -and $_.LastWriteTime -lt ($dates).end}
```

---

<div class="post-metadata">

**Author:** ![steve-jackson](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/steve-jackson/32/228_2.png) [@steve-jackson](https://forums.powershell.org/u/steve-jackson)\
**Post date:** [December 9, 2015, 1:45am UTC](https://forums.powershell.org/t/select-objects-in-folder-by-date-range/5332/3 "2015-12-09T01:45:51Z")

</div>

Thanks Curtis but that still doesn’t work. I am still getting no results returned and I have verified there are objects in the folder for if I manually run

Get-ChildItem -path “\server.domain.com\path”

it will return what is there and I can see the LastWriteTime property.

But when I try to add in the dates I get nothing.

---

<div class="post-metadata">

**Author:** ![lauras-juozulynas](https://avatars.discourse-cdn.com/v4/letter/l/6f9a4e/32.png) [@lauras-juozulynas](https://forums.powershell.org/u/lauras-juozulynas)\
**Post date:** [December 9, 2015, 1:49am UTC](https://forums.powershell.org/t/select-objects-in-folder-by-date-range/5332/4 "2015-12-09T01:49:31Z")

</div>

Hi,

Looking at last bit where you do logical compare makes me wonder.

You take an object (where-object) and then say that this object has to have LastWriteTime same as date.start and same as date.end. This is because you use “-and”. This is possible only if you select 1 day range.

I think you should say:  
Where-Object { $_.LastWriteTime -ge ($dates).start -and $_.LastWriteTime -le ($dates).end}

This would translate to LastWriteTime is same or higher than start date AND is lower or equal as date.end.

Didn’t test this 🙂

And ups, Curtis beat me to it 🙂

---

<div class="post-metadata">

**Author:** ![curtis-smith](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/curtis-smith/32/121_2.png) [@curtis-smith](https://forums.powershell.org/u/curtis-smith)\
**Post date:** [December 9, 2015, 1:54am UTC](https://forums.powershell.org/t/select-objects-in-folder-by-date-range/5332/5 "2015-12-09T01:54:27Z")

</div>

This is my run of your code with nothing more that the -gt -lt changes and a path that is local for me. I selected a range of Dec 1 - Dec 9. I also tested both PoSh 2.0 and 4.0. Worked in both versions.

```
Function Get-DateRange
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
out-null

$WinForm = New-Object Windows.Forms.Form
$WinForm.text = "Calendar Control"
$WinForm.Size = New-Object Drawing.Size(411,190)

$Calendar = New-Object System.Windows.Forms.MonthCalendar
$Calendar.MaxSelectionCount = 356
$Calendar.SetCalendarDimensions([int]2,[int]1)
$WinForm.Controls.Add($Calendar)

$WinForm.Add_Shown($WinForm.Activate())
$WinForm.showdialog() | Out-Null
$Calendar.SelectionRange
} #end function Get-DateRange

# ***Entry point to script***
$dates = Get-DateRange

#Get-ChildItem -path "\\server.domain.com\path" | Where-Object { $_.LastWriteTime -like ($dates).start -and $_.LastWriteTime -like ($dates).end}
Get-ChildItem -path "F:\Temp\Powershell\part" | Where-Object { $_.LastWriteTime -gt ($dates).start -and $_.LastWriteTime -lt ($dates).end}
```

**Results:**

```
Directory: F:\Temp\Powershell\part

Mode LastWriteTime Length Name                                                                                                                                                                               
---- ------------- ------ ----                                                                                                                                                                               
-a--- 12/2/2015 2:19 AM 174 test.ps1                                                                                                                                                                           
-a--- 12/2/2015 12:25 PM 79 FileP.txt.alsdfjlas                                                                                                                                                                
-a--- 12/2/2015 12:26 PM 88 FileD.txt.g65fg4                                                                                                                                                                   
-a--- 12/2/2015 12:26 PM 78 FileR.txt.af8a9etaf                                                                                                                                                                
-a--- 12/2/2015 12:26 PM 78 FileR.txt - Copy.af8a9etaf                                                                                                                                                         
-a--- 12/2/2015 12:25 PM 79 FileP.txt - Copy.alsdfjlas                                                                                                                                                         
-a--- 12/2/2015 12:26 PM 88 FileD.txt - Copy.g65fg4
```

---

<div class="post-metadata">

**Author:** ![steve-jackson](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/steve-jackson/32/228_2.png) [@steve-jackson](https://forums.powershell.org/u/steve-jackson)\
**Post date:** [December 9, 2015, 2:17am UTC](https://forums.powershell.org/t/select-objects-in-folder-by-date-range/5332/6 "2015-12-09T02:17:34Z")

</div>

Here is what I discovered…

If I just select a range up until today so 1-9 it returns no results.

However if I add one day into the future (december 1 - 10) then it returns the results

---

<div class="post-metadata">

**Author:** ![curtis-smith](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/curtis-smith/32/121_2.png) [@curtis-smith](https://forums.powershell.org/u/curtis-smith)\
**Post date:** [December 9, 2015, 2:24am UTC](https://forums.powershell.org/t/select-objects-in-folder-by-date-range/5332/7 "2015-12-09T02:24:37Z")

</div>

That means your files were last modified today. If you look at the values returned by the MonthCalendar object, you will see that it returns a datetime object at 00:00 hours (12:00 AM) for the date you select. So selecting today’s date as an ending date does not inlcude today’s files.

```
Function Get-DateRange
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
out-null

$WinForm = New-Object Windows.Forms.Form
$WinForm.text = "Calendar Control"
$WinForm.Size = New-Object Drawing.Size(411,190)

$Calendar = New-Object System.Windows.Forms.MonthCalendar
$Calendar.MaxSelectionCount = 356
$Calendar.SetCalendarDimensions([int]2,[int]1)
$WinForm.Controls.Add($Calendar)

$WinForm.Add_Shown($WinForm.Activate())
$WinForm.showdialog() | Out-Null
$Calendar.SelectionRange
} #end function Get-DateRange

# ***Entry point to script***
$dates = Get-DateRange
$dates | FT -AutoSize
```

**Results:**

```
Start End                  
----- ---                  
12/1/2015 12:00:00 AM 12/9/2015 12:00:00 AM
```

---

<div class="post-metadata">

**Author:** ![curtis-smith](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/curtis-smith/32/121_2.png) [@curtis-smith](https://forums.powershell.org/u/curtis-smith)\
**Post date:** [December 9, 2015, 2:30am UTC](https://forums.powershell.org/t/select-objects-in-folder-by-date-range/5332/8 "2015-12-09T02:30:47Z")

</div>

One easy work around is to add 1 day to your end value. That also means you should probably use -ge and -lt, rather than -gt and -lt in your Where-Object.

```
Function Get-DateRange
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
out-null

$WinForm = New-Object Windows.Forms.Form
$WinForm.text = "Calendar Control"
$WinForm.Size = New-Object Drawing.Size(411,190)

$Calendar = New-Object System.Windows.Forms.MonthCalendar
$Calendar.MaxSelectionCount = 356
$Calendar.SetCalendarDimensions([int]2,[int]1)
$WinForm.Controls.Add($Calendar)

$WinForm.Add_Shown($WinForm.Activate())
$WinForm.showdialog() | Out-Null
$Calendar.SelectionRange
} #end function Get-DateRange

# ***Entry point to script***
$dates = Get-DateRange
$dates | FT Start, End -AutoSize

$dates.Start
$dates.End
$dates.End.AddDays(1)
```

**Results:**

```
Start End                  
----- ---                  
12/1/2015 12:00:00 AM 12/9/2015 12:00:00 AM

Tuesday, December 01, 2015 12:00:00 AM
Wednesday, December 09, 2015 12:00:00 AM
Thursday, December 10, 2015 12:00:00 AM
```

---

<div class="post-metadata">

**Author:** ![steve-jackson](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/steve-jackson/32/228_2.png) [@steve-jackson](https://forums.powershell.org/u/steve-jackson)\
**Post date:** [December 9, 2015, 5:56am UTC](https://forums.powershell.org/t/select-objects-in-folder-by-date-range/5332/9 "2015-12-09T05:56:47Z")

</div>

Hi Curtis thanks again for your help, your suggestion ended up doing the trick for me.

---

<div class="post-metadata">

**Author:** ![daniel-matei](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/daniel-matei/32/589_2.png) [@daniel-matei](https://forums.powershell.org/u/daniel-matei)\
**Post date:** [September 5, 2016, 1:27am UTC](https://forums.powershell.org/t/select-objects-in-folder-by-date-range/5332/10 "2016-09-05T01:27:40Z")

</div>

Hello,

Interesting enough but I would like to use these kind of forms and specify the start time as well. Because I want to implement them in maintenance plans and usually those have specific times in them. For example:

Maintenance window: Saturday 10/09/2016 8:00AM -\> 11/09/2016 12PM  
How can one add the time as selection as well?

Thanks

---

<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:40pm UTC](https://forums.powershell.org/t/select-objects-in-folder-by-date-range/5332/11 "2024-05-16T20:40:36Z")

</div>

Closed on May 16, 2024, 8:40pm UTC


