フォルダサイズ調査スクリプト

フォルダサイズ調査スクリプト

どのフォルダが容量を食っているか確認するためのPowerShellスクリプト。ファイル名:check-folder-size.ps1

#
# フォルダサイズ調査スクリプト
# 使い方:
# .\check-folder-size.ps1 -Path "C:\MAIN"
#

param(
    [string]$Path = "C:\",
    [switch]$ExportCsv,
    [string]$OutputDir = "$env:USERPROFILE\Desktop"
)

# パス確認
if (-not (Test-Path $Path)) {
    Write-Host "エラー:パスが存在しません:$Path" -ForegroundColor Red
    exit 1
}

Write-Host "調査対象:$Path" -ForegroundColor Cyan

$startTime = Get-Date

$results = Get-ChildItem $Path -Directory -Force -ErrorAction SilentlyContinue |
    ForEach-Object {
        Write-Host "調査中:$($_.Name)..." -ForegroundColor Yellow -NoNewline

        $size = (Get-ChildItem $_.FullName -Recurse -File -Force -ErrorAction SilentlyContinue |
            Measure-Object -Property Length -Sum).Sum

        $sizeGB = if ($size) {
            [math]::Round($size / 1GB, 2)
        } else {
            0
        }

        Write-Host " → $sizeGB GB" -ForegroundColor Green

        [PSCustomObject]@{
            Folder   = $_.Name
            FullPath = $_.FullName
            SizeGB   = $sizeGB
            SizeMB   = if ($size) {
                [math]::Round($size / 1MB, 2)
            } else {
                0
            }
        }
    } | Sort-Object SizeGB -Descending

# CSV出力
if ($ExportCsv) {
    $folderName = (Split-Path $Path -Leaf) -replace '[\\/:*?"<>|]', '_'

    if ([string]::IsNullOrEmpty($folderName)) {
        $folderName = "Root"
    }

    $outputPath = Join-Path $OutputDir "FolderSize_${folderName}_$(Get-Date -Format 'yyyyMMdd_HHmmss').csv"

    $results | ConvertTo-Csv -NoTypeInformation | Out-File -FilePath $outputPath -Encoding UTF8

    Write-Host "CSV: $outputPath" -ForegroundColor Green
}

$totalTime = ((Get-Date) - $startTime).TotalMinutes

Write-Host "調査時間:$($totalTime.ToString('F1'))分" -ForegroundColor Cyan

$results | Format-Table -AutoSize

容量の大きいフォルダ

個人で意図的に使用しているフォルダ以外では、以下のフォルダの容量が大きい。

C:\Users\%profile%\AppData\Local
OneDrive

Angularの開発をしているとnodeのキャッシュもたまりがち、以下はかなり容量を食っていた。

C:\Users\%profile%\AppData\Local\node-chromium\Cache

最新のものだけ残して削除してOK。

コメント

タイトルとURLをコピーしました