关于PowerShellGet-ChildItem的坑-创新互联

起因

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

创新互联专业为企业提供霍林郭勒网站建设、霍林郭勒做网站、霍林郭勒网站设计、霍林郭勒网站制作等企业网站建设、网页设计与制作、霍林郭勒企业网站模板建站服务,10年霍林郭勒做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

代码

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

文章题目:关于PowerShellGet-ChildItem的坑-创新互联
转载注明:http://scyanting.com/article/dedogd.html