35

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

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

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

但我真正想要的是目录中所有文件的总大小,包括子目录。另外,我希望能够按大小对其进行排序,可选。

4

6 回答 6

40

“Exploring Beautiful Languages”博客中提供了一个实现:

“'du -s *' 在 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> 目录摘要

姓名总和
---- ---
DLL 4794012
文件 4160038
包括 382592
库 13752327
库 948600
tcl 3248808
工具 547784
许可证.txt 13817
新闻.txt 88573
python.exe 24064
pythonw.exe 24576
自述文件 56691
w9xpopen.exe 4608
于 2009-05-15T12:06:17.100 回答
28

我稍微修改了答案中的命令以按大小降序排序并以 MB 为单位包含大小:

gci . | 
  %{$f=$_; gci -r $_.FullName | 
    measure-object -property length -sum |
    select  @{Name="Name"; Expression={$f}}, 
            @{Name="Sum (MB)"; 
            Expression={"{0:N3}" -f ($_.sum / 1MB) }}, Sum } |
  sort Sum -desc |
  format-table -Property Name,"Sum (MB)", Sum -autosize

输出:

PS C:\scripts> du

Name                                 Sum (MB)       Sum
----                                 --------       ---
results                              101.297  106217913
SysinternalsSuite                    56.081    58805079
ALUC                                 25.473    26710018
dir                                  11.812    12385690
dir2                                 3.168      3322298

也许这不是最有效的方法,但它确实有效。

于 2012-10-10T16:23:53.427 回答
4
function Get-DiskUsage ([string]$path=".") {
    $groupedList = Get-ChildItem -Recurse -File $path | Group-Object directoryName | select name,@{name='length'; expression={($_.group | Measure-Object -sum length).sum } }
    foreach ($dn in $groupedList) {
        New-Object psobject -Property @{ directoryName=$dn.name; length=($groupedList | where { $_.name -like "$($dn.name)*" } | Measure-Object -Sum length).sum }
    }
}

我的有点不同;我将目录名上的所有文件分组,然后遍历该列表,为每个目录构建总计(包括子目录)。

于 2016-02-12T15:19:13.143 回答
4

基于以前的答案,这将适用于那些想要以 KB、MB、GB 等显示大小并且仍然能够按大小排序的人。要更改单位,只需将“MB”更改为“Name=”和“Expression=”中的所需单位。您还可以通过更改“2”来更改要显示的小数位数(四舍五入)。

function du($path=".") {
    Get-ChildItem $path |
    ForEach-Object {
        $file = $_
        Get-ChildItem -File -Recurse $_.FullName | Measure-Object -Property length -Sum |
        Select-Object -Property @{Name="Name";Expression={$file}},
                                @{Name="Size(MB)";Expression={[math]::round(($_.Sum / 1MB),2)}} # round 2 decimal places
    }
}

这将大小作为数字而不是字符串提供(如另一个答案所示),因此可以按大小排序。例如:

PS C:\Users\merce> du | Sort-Object -Property "Size(MB)" -Descending

Name      Size(MB)
----      --------
OneDrive  30944.04
Downloads    401.7
Desktop     335.07
.vscode     301.02
Intel         6.62
Pictures      6.36
Music         0.06
Favorites     0.02
.ssh          0.01
Searches         0
Links            0
于 2020-07-02T22:17:19.083 回答
3

如果您只需要该路径的总大小,可以使用一个简化版本,

Get-ChildItem -Recurse ${HERE_YOUR_PATH} | Measure-Object -Sum Length
于 2020-11-21T04:28:42.940 回答
0

我自己使用以前的答案:

function Format-FileSize([int64] $size) {
    if ($size -lt 1024)
    {
        return $size
    }
    if ($size -lt 1Mb)
    {
        return "{0:0.0} Kb" -f ($size/1Kb)
    }
    if ($size -lt 1Gb)
    {
        return "{0:0.0} Mb" -f ($size/1Mb)
    }
    return "{0:0.0} Gb" -f ($size/1Gb)
}

function du {
        param(
        [System.String]
        $Path=".",
        [switch]
        $SortBySize,
        [switch]
        $Summary
    )
    $path = (get-item ".").FullName
    $groupedList = Get-ChildItem -Recurse -File $Path | 
        Group-Object directoryName | 
            select name,@{name='length'; expression={($_.group | Measure-Object -sum length).sum } }
    $results = ($groupedList | % {
        $dn = $_
        if ($summary -and ($path -ne $dn.name)) {
            return
        }
        $size = ($groupedList | where { $_.name -like "$($dn.name)*" } | Measure-Object -Sum length).sum
        New-Object psobject -Property @{ 
            Directory=$dn.name; 
            Size=Format-FileSize($size);
            Bytes=$size` 
        }
    })
    if ($SortBySize)
        { $results = $results | sort-object -property Bytes }
    $results | more
}
于 2021-02-07T12:24:20.420 回答