-
-
Notifications
You must be signed in to change notification settings - Fork 544
Expand file tree
/
Copy pathcheck-drives.ps1
More file actions
executable file
·59 lines (56 loc) · 1.88 KB
/
check-drives.ps1
File metadata and controls
executable file
·59 lines (56 loc) · 1.88 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
<#
.SYNOPSIS
Checks the drive space
.DESCRIPTION
This PowerShell script queries the free space of all drives and prints it.
.PARAMETER minLevel
Specifies the minimum warning level (5GB by default)
.EXAMPLE
PS> ./check-drives.ps1
✅ Drive C: uses 53% of 930GB (521GB free), D: uses 87% of 4TB (1TB free), E: is empty
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([int64]$minLevel = 5GB)
function Bytes2String { param([int64]$bytes)
if ($bytes -lt 1KB) { return "$bytes bytes" }
if ($bytes -lt 1MB) { return '{0:N0}KB' -f ($bytes / 1KB) }
if ($bytes -lt 1GB) { return '{0:N0}MB' -f ($bytes / 1MB) }
if ($bytes -lt 1TB) { return '{0:N0}GB' -f ($bytes / 1GB) }
if ($bytes -lt 1PB) { return '{0:N0}TB' -f ($bytes / 1TB) }
return '{0:N0}GB' -f ($bytes / 1PB)
}
try {
Write-Progress "Querying drives..."
$drives = Get-PSDrive -PSProvider FileSystem
Write-Progress -completed "Done."
$status = "✅"
$reply = "Drive "
foreach($drive in $drives) {
$details = (Get-PSDrive $drive.Name)
if ($IsLinux) { $name = $drive.Name } else { $name = $drive.Name + ":" }
[int64]$free = $details.Free
[int64]$used = $details.Used
[int64]$total = ($used + $free)
if ($reply -ne "Drive ") { $reply += ", " }
if ($total -eq 0) {
$reply += "$name is empty"
} elseif ($free -eq 0) {
$status = "⚠️"
$reply += "$name with ($(Bytes2String $total)) is FULL"
} elseif ($free -lt $minLevel) {
$status = "⚠️"
$reply += "$name nearly full ($(Bytes2String $free) of $(Bytes2String $total) left)"
} else {
[int64]$percent = ($used * 100) / $total
$reply += "$name uses $percent% of $(Bytes2String $total) ($(Bytes2String $free) free)"
}
}
Write-Host "$status $reply"
exit 0 # success
} catch {
"⚠️ ERROR: $($Error[0]) (at line $($_.InvocationInfo.ScriptLineNumber))"
exit 1
}