skip to content

Search

Syspirit
EN

Disk Space

Commands and tools to analyze, monitor, and manage disk space on Windows!

Windows
Published on
Karl Certa

Maintain and manage storage space on a Windows system using PowerShell, CMD, and built-in Windows tools.

📊 Storage Overview

📋 Disk space per drive

Get-WmiObject -Class Win32_LogicalDisk | Select-Object DeviceID, @{Name="Size(GB)";Expression={[math]::Round($_.Size/1GB,2)}}, @{Name="FreeSpace(GB)";Expression={[math]::Round($_.FreeSpace/1GB,2)}}
wmic logicaldisk get size,freespace,caption

💽 Modern disk space view

Get-Volume | Format-Table DriveLetter, FileSystemLabel, @{Name="Size(GB)";Expression={[math]::Round($_.Size/1GB,2)}}, @{Name="Free(GB)";Expression={[math]::Round($_.SizeRemaining/1GB,2)}}

💿 Detailed disk information

Get-Disk | Format-Table Number, FriendlyName, @{Name="Size(GB)";Expression={[math]::Round($_.Size/1GB,2)}}, HealthStatus
diskpart
list disk

📂 Directory Analysis

📦 Directory size (PowerShell)

(Get-ChildItem -Path "C:\Windows" -Recurse | Measure-Object -Property Length -Sum).Sum / 1GB

📊 Size of all folders in current directory

Get-ChildItem -Directory | ForEach-Object { $size = (Get-ChildItem -Path $_.FullName -Recurse | Measure-Object -Property Length -Sum).Sum / 1MB; "$($_.Name): $([math]::Round($size,2)) MB" }

🧱 Directories by descending size

Get-ChildItem -Directory | ForEach-Object {
    $size = (Get-ChildItem -Path $_.FullName -Recurse -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).Sum / 1MB
    [PSCustomObject]@{Name=$_.Name; Size=$size}
} | Sort-Object Size -Descending

📁 Directory contents with sizes

dir /s

🏆 Identifying Large Files

🔍 Top 10 largest files

Get-ChildItem -Path "C:\" -Recurse -File -ErrorAction SilentlyContinue | Sort-Object Length -Descending | Select-Object -First 10 FullName, @{Name="Size(MB)";Expression={[math]::Round($_.Length/1MB,2)}}

🐋 Large files (+100MB)

Get-ChildItem -Path "C:\" -Recurse -File -ErrorAction SilentlyContinue | Where-Object {$_.Length -gt 100MB} | Select-Object FullName, @{Name="Size(MB)";Expression={[math]::Round($_.Length/1MB,2)}}

🗂️ Largest files by extension

Get-ChildItem -Path "C:\" -Recurse -File -ErrorAction SilentlyContinue | Group-Object Extension | Sort-Object @{Expression={($_.Group | Measure-Object Length -Sum).Sum}} -Descending | Select-Object -First 10

📁 Largest directories

Get-ChildItem -Path "C:\" -Directory | ForEach-Object {
    $size = (Get-ChildItem -Path $_.FullName -Recurse -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).Sum / 1GB
    [PSCustomObject]@{Path=$_.FullName; Size=$size}
} | Sort-Object Size -Descending | Select-Object -First 10

🧹 Cleanup and Freeing Space

🗑️ Windows Disk Cleanup

cleanmgr /sagerun:1

🗑️ Empty recycle bins

Clear-RecycleBin -Force

📜 Clean temporary folders

Remove-Item -Path "$env:TEMP\*" -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item -Path "C:\Windows\Temp\*" -Recurse -Force -ErrorAction SilentlyContinue

🧹 Clean old files (>30 days)

Get-ChildItem -Path "C:\Logs" -Recurse -File | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-30)} | Remove-Item -Force

💿 Clean restore points

vssadmin delete shadows /for=c: /oldest

🚨 Real-time Monitoring

📊 Continuous space monitoring

while ($true) {
    Clear-Host
    Get-Volume | Format-Table DriveLetter, FileSystemLabel, @{Name="Size(GB)";Expression={[math]::Round($_.Size/1GB,2)}}, @{Name="Free(GB)";Expression={[math]::Round($_.SizeRemaining/1GB,2)}}
    Start-Sleep -Seconds 10
}

🔄 Large directory monitoring

$folders = @("C:\Windows\Logs", "C:\ProgramData", "C:\Users")
while ($true) {
    Clear-Host
    foreach ($folder in $folders) {
        $size = (Get-ChildItem -Path $folder -Recurse -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).Sum / 1GB
        Write-Host "$folder : $([math]::Round($size,2)) GB"
    }
    Start-Sleep -Seconds 30
}

📋 Advanced Log Management

📝 Windows Event Logs

Managing Windows system logs to purge and limit used space

Cleanup commands

# Check event log sizes
Get-WinEvent -ListLog * | Where-Object {$_.RecordCount -gt 0} | Select-Object LogName, @{Name="Size(MB)";Expression={[math]::Round($_.FileSize/1MB,2)}}, RecordCount | Sort-Object "Size(MB)" -Descending
 
# Clear a specific log
Clear-EventLog -LogName "Application"
Clear-EventLog -LogName "System"
Clear-EventLog -LogName "Security"
 
# Clear all logs (caution!)
Get-EventLog -List | ForEach-Object {Clear-EventLog $_.Log}
 
# Backup before clearing
Get-EventLog -LogName Application | Export-Csv -Path "C:\Backup\Application.csv"

Configuration via Group Policy

# Open local policy editor
gpedit.msc
 
# Path: Computer Configuration > Administrative Templates > Windows Components > Event Viewer
# Settings: Maximum log file size

🔄 IIS Logs (if applicable)

Automatic IIS log rotation to prevent unlimited growth

# Locate IIS logs
Get-ChildItem -Path "C:\inetpub\logs\LogFiles" -Recurse
 
# Delete old IIS logs (>30 days)
Get-ChildItem -Path "C:\inetpub\logs\LogFiles" -Recurse -File | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-30)} | Remove-Item -Force
 
# Configure automatic rotation via IIS Manager
# IIS Manager > Sites > Site > Logging > Format: W3C > Advanced > Log File Rollover

🔍 Windows Update Logs

Windows Update log cleanup

# Clean update files
Remove-Item -Path "C:\Windows\SoftwareDistribution\Download\*" -Recurse -Force -ErrorAction SilentlyContinue
 
# Clean Windows Update cache
Stop-Service -Name wuauserv
Stop-Service -Name cryptSvc
Stop-Service -Name bits
Stop-Service -Name msiserver
Remove-Item -Path "C:\Windows\SoftwareDistribution" -Recurse -Force -ErrorAction SilentlyContinue
Start-Service -Name wuauserv
Start-Service -Name cryptSvc
Start-Service -Name bits
Start-Service -Name msiserver

🧹 Manual Log File Cleanup

Commands to manually purge log files and reclaim space

# Delete old .log files
Get-ChildItem -Path "C:\Windows\Logs" -Recurse -File -Filter "*.log" | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-30)} | Remove-Item -Force
 
# Delete dump files
Remove-Item -Path "C:\Windows\Minidump\*" -Force -ErrorAction SilentlyContinue
Remove-Item -Path "C:\Windows\memory.dmp" -Force -ErrorAction SilentlyContinue
 
# Clean application logs
$logPaths = @(
    "C:\Windows\System32\LogFiles",
    "C:\Windows\debug",
    "C:\ProgramData\Microsoft\Windows\WER"
)
foreach ($path in $logPaths) {
    if (Test-Path $path) {
        Get-ChildItem -Path $path -Recurse -File | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-7)} | Remove-Item -Force -ErrorAction SilentlyContinue
    }
}

🛠️ Built-in Graphical Tools

💾 Disk Cleanup

# Open disk cleanup tool
cleanmgr
 
# Advanced cleanup with system options
cleanmgr /sageset:1
cleanmgr /sagerun:1

📊 Storage Usage Analyzer

# Open Windows 10/11 storage analyzer
ms-settings:storagesense