-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDiskUsageAnalyzer.ps1
More file actions
87 lines (70 loc) · 2.73 KB
/
DiskUsageAnalyzer.ps1
File metadata and controls
87 lines (70 loc) · 2.73 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<#
.SYNOPSIS
Analyzes disk usage for specified directories and generates a folder size report.
.DESCRIPTION
This script scans specified directories, calculates the sizes of each folder,
and generates a detailed report sorted by size. The report can be displayed in
the console or exported to a CSV file.
.NOTES
Author: Emanuele Bartolesi
Version: 1.0
Created: 2024-11-17
.PARAMETER Directory
The directory to analyze. Defaults to the current directory.
.PARAMETER ExportPath
The path to save the report as a CSV file. Optional.
.PARAMETER MinSize
Minimum size (in MB) to include in the report. Defaults to 0 MB.
.EXAMPLE
./DiskUsageAnalyzer.ps1 -Directory "C:\Users\YourName\Documents"
.EXAMPLE
./DiskUsageAnalyzer.ps1 -Directory "C:\Users" -ExportPath "C:\Reports\DiskUsageReport.csv" -MinSize 10
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $false)]
[string]$Directory = (Get-Location).Path,
[Parameter(Mandatory = $false)]
[string]$ExportPath,
[Parameter(Mandatory = $false)]
[int]$MinSize = 0
)
# Function to calculate folder size
function Get-FolderSize {
param (
[string]$FolderPath
)
$Size = (Get-ChildItem -Path $FolderPath -Recurse -ErrorAction SilentlyContinue |
Measure-Object -Property Length -Sum).Sum
return [math]::Round($Size / 1MB, 2) # Convert to MB
}
# Main script execution
try {
Write-Host "Analyzing disk usage for: $Directory" -ForegroundColor Cyan
if (-not (Test-Path $Directory)) {
throw "The specified directory does not exist: $Directory"
}
$Folders = Get-ChildItem -Path $Directory -Directory -Force | ForEach-Object {
$Size = Get-FolderSize -FolderPath $_.FullName
[PSCustomObject]@{
Folder = $_.FullName
SizeInMB = $Size
ItemCount = (Get-ChildItem -Path $_.FullName -Recurse -ErrorAction SilentlyContinue | Measure-Object).Count
}
}
# Filter by minimum size
$FilteredFolders = $Folders | Where-Object { $_.SizeInMB -ge $MinSize } | Sort-Object -Property SizeInMB -Descending
# Display results
Write-Host "Disk Usage Report:" -ForegroundColor Green
$FilteredFolders | Format-Table -AutoSize
# Export results if needed
if ($ExportPath) {
Write-Host "Exporting report to: $ExportPath" -ForegroundColor Yellow
$FilteredFolders | Export-Csv -Path $ExportPath -NoTypeInformation -Encoding UTF8
Write-Host "Report exported successfully!" -ForegroundColor Green
}
} catch {
Write-Warning "An error occurred: $_"
} finally {
Write-Host "Disk usage analysis completed." -ForegroundColor Cyan
}