且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

未列出子目录的文件夹的 Powershell 文件夹大小

更新时间:2023-08-21 20:11:28

需要递归获取每个目录的总内容大小来输出.此外,您需要指定要抓取的内容不是目录,否则可能会出错(因为目录没有 Length 参数).

You need to get the total contents size of each directory recursively to output. Also, you need to specify that the contents you're grabbing to measure are not directories, or you risk errors (as directories do not have a Length parameter).

这是为您要查找的输出修改的脚本:

Here's your script modified for the output you're looking for:

$colItems = Get-ChildItem $startFolder | Where-Object {$_.PSIsContainer -eq $true} | Sort-Object
foreach ($i in $colItems)
{
    $subFolderItems = Get-ChildItem $i.FullName -recurse -force | Where-Object {$_.PSIsContainer -eq $false} | Measure-Object -property Length -sum | Select-Object Sum
    $i.FullName + " -- " + "{0:N2}" -f ($subFolderItems.sum / 1MB) + " MB"
}