1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
| ## 定义要检查的路径
$Path = "D:\"
## 获取D盘根目录下的一级子文件夹
$Folders = Get-ChildItem -Path $Path -Directory
## 循环遍历每个文件夹并计算大小
$FolderSizes = @()
foreach ($Folder in $Folders) {
Write-Host "正在计算文件夹: $($Folder.FullName)"
## 获取文件夹下的所有文件(递归)
$Items = Get-ChildItem -Path $Folder.FullName -Recurse -ErrorAction SilentlyContinue | Where-Object { -not $_.PSIsContainer }
## 计算文件总大小 (以字节为单位)
$SizeInBytes = ($Items | Measure-Object -Property Length -Sum).Sum
## 转换为其他单位 (例如MB, GB)
$SizeInGB = $SizeInBytes / 1GB
## 将结果添加到数组中
$FolderSizes += [PSCustomObject]@{
Name = $Folder.Name
FullName = $Folder.FullName
Size_Bytes = $SizeInBytes
Size_GB = [Math]::Round($SizeInGB, 2)
}
}
## 按大小降序排序并输出结果
$FolderSizes | Sort-Object -Property Size_GB -Descending | Format-Table -AutoSize
|