Mike P
Mike P

Reputation: 27

How to list files created since 5:00PM yesterday?

Having trouble getting this command to work. I'm attempting to see all files with a specific string in the name created since exactly 5:00PM yesterday, no matter what time I run the script today. Below is what I have so far. Any help would be greatly appreciated.

$today = (get-date).dayofyear
$string = "blah"
$filepath = "C:\files"

Get-ChildItem -Recurse -Force $filePath | where { $_.CreationTime.dayofyear -eq (($today)-1) } -and { $_.CreationTime.hour -ge 17 | Where-Object { ($_.PSIsContainer -eq $false) -and ( $_.Name -like "*$string*"}

Upvotes: 0

Views: 1997

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200273

Get just the Date portion of the DateTime value (which gives you today at 0:00:00) and subtract 7 hours from that to get yesterday at 5 pm.

$cutoff = (Get-Date).Date.AddHours(-7)

Get-ChildItem -Recurse -Force $filePath | Where-Object {
  -not $_.PSIsContainer -and
  $_.CreationTime -ge $cutoff -and
  $_.Name -like "*$string*"
}

If you have PowerShell v3 or newer you can replace the condition -not $_.PSIsContainer by calling Get-ChildItem with the parameter -File.

Upvotes: 1

Related Questions