Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get files between date range in windows PowerShell

I'm trying to get files between a date range in windows power shell but it's getting all the files instead of just the ones in range.

Here's my commands:

[datetime]$start = '2018-04-01 00:00:00'
[datetime]$end = '2018-05-01 00:00:00'
Get-ChildItem "C:\Users\PC- 1\Downloads" | Where-Object { $_.LastWriteTime -gt $start -or $_.LastWriteTime -lt $end }
like image 261
Mohammad Yusuf Avatar asked Nov 06 '25 04:11

Mohammad Yusuf


1 Answers

You'll want to use the -and operator instead of -or to express "start < LastWriteTime < end"

[datetime]$start = '2018-04-01 00:00:00'
[datetime]$end = '2018-05-01 00:00:00'
Get-ChildItem "C:\Users\PC- 1\Downloads" |
  Where-Object { $_.LastWriteTime -gt $start -and $_.LastWriteTime -lt $end }
like image 92
Tung Avatar answered Nov 08 '25 01:11

Tung