如何列出昨天下午5点以来创建的文件?

时间:2016-02-12 17:10:09

标签: powershell

无法使此命令生效。我试图查看自昨天下午5点以来创建的名称中包含特定字符串的所有文件,无论我今天何时运行该脚本。以下是我到目前为止的情况。任何帮助将不胜感激。

$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*"}

1 个答案:

答案 0 :(得分:0)

只获取Date值的DateTime部分(今天0:00:00给你)并从中减去7小时,以便在昨天下午5点减去。

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

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

如果您有PowerShell v3或更新版本,则可以使用参数-not $_.PSIsContainer调用Get-ChildItem来替换条件-File