当我执行dir
命令时,它会提供以下信息:
02/11/2015 01:39 PM <DIR> bar
11/11/2014 07:22 AM <DIR> buz
02/12/2015 01:35 PM 140,660,736 foo.sdf
01/21/2015 02:04 PM 10,505 foo.sln
01/21/2015 02:04 PM 256 foo.vssscc
是否有一个开关或者甚至一个 PowerShell 命令可以列出所有目录和它们各自的尺寸?
答案1
在 PowerShell 中很简单。
$FolderSize = Get-ChildItem $FolderPath -Recurse -Force | Measure-Object -Property Length -Sum;
$FolderSize.Sum; #Size in bytes
$FolderSize.Sum / 1MB; #Size in MB
$FolderSize.Sum / 1GB; #Size in GB
要获取指定文件夹中的每个文件夹并计算每个文件夹,只需对它们进行迭代:
$BaseFolder = Get-ChildItem 'C:\Path\To\Folder';
$Results = @();
foreach ($f in $BaseFolder) {
if ($f.PSIsContainer -eq $true) {
$Size = Get-ChildItem $f -Recurse -Force | Measure-Object -Property Length -Sum;
}
else {
$Size = Get-ChildItem $f | Measure-Object -Property Length -Sum;
}
$Results += New-Object PSObject -Property @{Name = $f.Name; Length = $Size.Sum;}
}
$Results | Format-Table -AutoSize;