如何在 PowerShell 中獲取包含子資料夾的資料夾大小
Sheeraz Gul
2025年2月25日
PowerShell
PowerShell Folder

本教程展示了如何使用 PowerShell 獲得資料夾的大小,包括子資料夾。
在 PowerShell 中獲取包含子資料夾的資料夾的大小
要獲取資料夾的大小,我們需要一起運行幾個命令。首先,我們需要獲取指定資料夾的子項,測量資料夾的長度屬性,最後顯示 Sum
對象。
請參見以下步驟:
獲取指定資料夾子項的命令是:
Get-ChildItem "C:\Users"
上述命令將顯示指定資料夾的子項。在這裡,子項是指子資料夾和其他項目:
Directory: C:\Users
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 1/31/2022 1:24 PM DefaultAppPool
d-r--- 4/26/2021 3:32 PM Public
d----- 11/1/2022 1:33 PM Sheeraz
-a---- 7/14/2022 2:33 PM 224 Example.java
如我們所見,該資料夾有三個子資料夾和一個文件。現在,為了獲取所有子資料夾的項目,我們需要運行以下命令:
Get-ChildItem -Path "C:\Users" -Recurse -ErrorAction SilentlyContinue |
然後,為了測量所有子資料夾和項目的大小,我們需要運行以下命令:
Measure-Object -Property Length -Sum |
最後,在測量資料夾所有內容的長度屬性後,現在顯示該對象:
Select-Object Sum
總和對象將顯示資料夾的大小,包括子資料夾。要顯示資料夾中的項目數,我們使用 Count
對象:
Select-Object Count
上述命令將顯示資料夾 Sheeraz
的大小以 MBS 為單位。請參見輸出:
36304.1059274673 MB
Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
作者: Sheeraz Gul
Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.
LinkedIn Facebook