windowspowershellpowershell-5.0

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 }

Solution

  • 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 }