用 PowerShell 找出文件夹中最大的文件

按大小从大到小递归列出文件,快速定位占用磁盘空间的大文件。

Windows磁盘清理文件PowerShell低风险
运行 Get-ChildItem -Recurse -File | Sort-Object Length -Descending | Select-Object -First 20,即可列出最大的 20 个文件。

01命令

命令
$ Get-ChildItem -Path C:\Users\me -Recurse -File | Sort-Object Length -Descending | Select-Object -First 20 Length, FullName

02作用说明

Get-ChildItem 返回的每个文件都带有 Length 属性(字节),对它降序排序再取前 N 条,就能立刻看到哪些文件最占空间。可在 Select-Object 中加 @{n='MB';e={[math]::Round($_.Length/1MB,1)}} 把字节换算成 MB。相比安装 WinDirStat/TreeSize,这是一次性排查更快的办法。

03风险

风险低风险
使用条件PowerShell
!注意: 递归扫描整个 C:\ 会很慢且出现大量拒绝访问的报错,建议加 -ErrorAction SilentlyContinue,并把 -Path 指向用户或项目目录。

04撤销

✓无需撤销——该命令不会改变持久状态。

05来源

  • Get-ChildItem | Microsoft Learn
    Microsoft · vendor-doc · 2026-09-17
    依据: Microsoft documents Get-ChildItem with -Recurse and -File, and that FileInfo objects expose the Length property, which can be sorted with Sort-Object.
  • Get-ChildItem - PowerShell
    SS64 · technical · 2026-09-17
    依据: SS64 shows Get-ChildItem piped into Sort-Object and Select-Object to rank files, e.g. by file size.

最后验证: 2026-09-17 · 分类: 文件与文件夹

06相关方案