原贴地址:https://www.pstips.net/accessing-files-and-directories.html

只列出目录::

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" } 前面的例子(识别对象类型)是目前速度最快的,而后面的(文本比较)比较复杂和低效。

Where-Object也可以根据其它属性来过滤。

比如下面的例子通过管道过滤2007年5月12日后更改过的文件:

1 Dir | Where-Object { $_.CreationTime -gt [datetime]::Parse("May 12, 2007") } 也可以使用相对时间获取2周以内更改过的文件:

1 Dir | Where-Object { $_.CreationTime -gt (Get-Date).AddDays(-14) }