使用Where-Object也可以根据其它属性来过滤。
Dir | Where-Object { $_.CreationTime -gt [datetime]::Parse("May 12, 2007") }
Dir | Where-Object { $_.CreationTime -gt (Get-Date).AddDays(-14) }
- 以下命令查找 Program Files 文件夹中上次修改日期晚于 2005 年 10 月 1 日并且既不小于 1 MB 也不大于 10 MB 的所有可执行文件:
Get-ChildItem -Path $env:ProgramFiles -Recurse -Include *.exe | Where-Object -FilterScript {($_.LastWriteTime -gt "2005-10-01") -and ($_.Length -ge 1m) -and ($_.Length -le 10m)}
$deleteFiles = Get-Childitem $fullTargetPath -Recurse| Where {$_.LastWriteTime -lt (Get-Date).AddDays(-10)} | Remove-Item -Force
查询上一次写入时间为10天以前的文件,逐一进行删除
$deleteFiles = Get-Childitem $fullTargetPath -Recurse | Where {$_.LastWriteTime -lt (Get-Date).AddDays(-10)} | Foreach { Remove-Item $_.FullName -Force}
获取空文件:
$a = Get-ChildItem D:/ -Recurse | Where-Object {$_.PsIsContainer -eq $true}
$a | Where-Object{$_.GetDirectories().Count -eq 0 -and $_.GetFiles().Count -eq 0} | Select-Object FullName
向命令、函数和文件脚本传递文件
- # 只列出目录::
Dir | Where-Object { $_ -is [System.IO.DirectoryInfo] }
Dir | Where-Object { $_.PSIsContainer }
Dir | Where-Object { $_.Mode.Substring(0,1) -eq "d" }
- # 只列出文件:
Dir | Where-Object { $_ -is [System.IO.FileInfo] }
Dir | Where-Object { $_.PSIsContainer -eq $false}
Dir | Where-Object { $_.Mode.Substring(0,1) -ne "d" }