关于PowerShell Get-ChildItem的坑

起因

测试服务器只有C盘,固定大小,多人共享,每月重启升级,有时候会出现磁盘空间不足的问题,所有需要查找某台服务器上的大文件

代码

Get-ChildItem "c:" -Recurse | sort -descending -property length | select -first 10 name, Length

Run

可以说,相当慢了......

优化

最直观的就是排除C:Windows文件夹,然后再递归遍历其他文件(夹),看一下

Get-ChildItem "c:"  -Exclude "Windows"

运行了没输出。
难道Exclude的使用方式不对吗?

说明使用没有问题,可是why?

原因

google了下,看到有人给官方提的issue,官方也标记为Bug了。
https://github.com/PowerShell/PowerShell/issues/11649
https://stackoverflow.com/questions/38269209/using-get-childitem-exclude-or-include-returns-nothing/38308796

FIX

Get-ChildItem "c:"  -Exclude "Windows"

替换为

Get-Item c:* -Exclude "Windows"

最终代码

Get-Item c:* -Exclude "Windows" | Get-ChildItem  -Recurse | sort -descending -property length | select -first 10 name, Length
原文地址:https://www.cnblogs.com/talentzemin/p/14663437.html