比 XX 分钟前新的文件的测试路径

Test-Path for files newer than XX minutes ago

我有这个:

$now = get-date
Test-Path -NewerThan $now.AddMinutes(-130) 'D:\FileFolderLocation\*' |
  Out-File 'D:\OutputFolderLocation\checker.txt'

如果文件夹中存在比 130 分钟前更新的文件,则输出 True 或 False。

这与 PowerShell 3.0+ 配合使用效果很好,但我 运行 这是在装有 PowerShell 2.0 的服务器上使用的。是否可以在不使用 -NewerThan 的情况下重写它以使用 PowerShell 2.0?

当然可以。只需使用 Get-ChildItemWhere-Object 过滤器:

$now = Get-Date
[bool](Get-ChildItem 'D:\FileFolderLocation' |
  Where-Object { $_.LastWriteTime -gt $now.AddMinutes(-130) }) |
  Out-File 'D:\OutputFolderLocation\checker.txt'

将结果转换为 bool 会生成 $true$false,具体取决于是否找到了匹配的文件。如果您需要排除目录,请将 -and -not $_.PSIsContainer 添加到过滤器。