Windows PowerShell中的Windows?

如何使用PowerShell进行 du-ish分析?我想定期检查磁盘上目录的大小。

以下给出了当前目录中每个文件的大小:

foreach ($o in gci)
{
   Write-output $o.Length
}

但我真正想要的是目录中所有文件的聚合大小,包括子目录。此外,我也希望能够根据大小进行排序。

博客中的“探索美丽语言”有一个实现:

“An implementation of ‘du -s *’ in Powershell”

function directory-summary($dir=".") { 
  get-childitem $dir | 
    % { $f = $_ ; 
        get-childitem -r $_.FullName | 
           measure-object -property length -sum | 
             select @{Name="Name";Expression={$f}},Sum}
}

(代码由博主:Luis Diego Fallas)

输出

PS C:\Python25> directory-summary

Name                  Sum
----                  ---
DLLs              4794012
Doc               4160038
include            382592
Lib              13752327
libs               948600
tcl               3248808
Tools              547784
LICENSE.txt         13817
NEWS.txt            88573
python.exe          24064
pythonw.exe         24576
README.txt          56691
w9xpopen.exe         4608

相关文章

Windows2012R2备用域控搭建 前置操作 域控主域控的主dns:自...
主域控角色迁移和夺取(转载) 转载自:http://yupeizhi.blo...
Windows2012R2 NTP时间同步 Windows2012R2里没有了internet时...
Windows注册表操作基础代码 Windows下对注册表进行操作使用的...
黑客常用WinAPI函数整理之前的博客写了很多关于Windows编程的...
一个简单的Windows Socket可复用框架说起网络编程,无非是建...