用 PowerShell 查看文件夹总大小(Windows 版 du)
用 Get-ChildItem 递归列出文件,再用 Measure-Object Length -Sum 求和,得到文件夹占用的磁盘空间。
运行 (Get-ChildItem 'C:\文件夹' -Recurse -File -Force | Measure-Object Length -Sum).Sum 并除以 1MB 换算成 MB。
01命令
命令
$ "{0:N2} MB" -f ((Get-ChildItem 'C:\Users\me\Downloads' -Recurse -File -Force -ErrorAction SilentlyContinue |
Measure-Object Length -Sum).Sum / 1MB)02作用说明
资源管理器的文件夹属性能看到大小,但命令行下 Windows 版的 du 就是把 Get-ChildItem -Recurse -File 管道给 Measure-Object Length -Sum。加 -Force 可以包含隐藏文件,-ErrorAction SilentlyContinue 则会跳过无权限读取的文件夹(比如受保护的系统目录)而不是中途报错退出。原始字节数除以 1MB 或 1GB 才方便阅读;想一次比较多个文件夹,可以按顶级目录分组再分别求和。
03风险
!注意: 对很大的目录树(如 C:\Windows 或整个用户目录)统计可能耗时数分钟并大量读盘。
04撤销
✓无需撤销——该命令不会改变持久状态。
05来源
- Measure-Object依据: Measure-Object Length -Sum totals the file sizes piped from Get-ChildItem, the standard way to compute folder size in PowerShell.
- Measure-Object - SS64依据: Shows -Sum aggregation of numeric properties like file Length.
最后验证: 2026-09-18 · 分类: 文件与文件夹