Mohammad Yusuf
Mohammad Yusuf

Reputation: 17054

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 }

Upvotes: 4

Views: 8466

Answers (1)

Tung
Tung

Reputation: 5434

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 }

Upvotes: 7

Related Questions