-
-
Notifications
You must be signed in to change notification settings - Fork 432
/
check-drive-space.ps1
executable file
·58 lines (53 loc) · 1.92 KB
/
check-drive-space.ps1
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
<#
.SYNOPSIS
Checks the drive space
.DESCRIPTION
This PowerShell script checks the given drive for free space left (10 GB by default).
.PARAMETER driveName
Specifies the drive name to check (e.g. "C")
.PARAMETER minLevel
Specifies the minimum level in bytes (10GB by default)
.EXAMPLE
PS> ./check-drive-space.ps1 C
✅ Drive C: uses 56% of 1TB - 442GB free
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([string]$driveName = "", [int64]$minLevel = 10 * 1000 * 1000) # GB
function Bytes2String { param([int64]$bytes)
if ($bytes -lt 1000) { return "$bytes bytes" }
$bytes /= 1000
if ($bytes -lt 1000) { return "$($bytes)KB" }
$bytes /= 1000
if ($bytes -lt 1000) { return "$($bytes)MB" }
$bytes /= 1000
if ($bytes -lt 1000) { return "$($bytes)GB" }
$bytes /= 1000
if ($bytes -lt 1000) { return "$($bytes)TB" }
$bytes /= 1000
return "$($bytes)PB"
}
try {
if ($driveName -eq "" ) { $driveName = Read-Host "Enter the drive name to check" }
$details = (Get-PSDrive $driveName)
if (-not $IsLinux) { $driveName = $driveName + ":" }
[int64]$free = $details.Free
[int64]$used = $details.Used
[int64]$total = ($used + $free)
if ($total -eq 0) {
Write-Host "✅ Drive $driveName is empty"
} elseif ($free -eq 0) {
Write-Host "⚠️ Drive $driveName with $(Bytes2String $total) is full"
} elseif ($free -lt $minLevel) {
Write-Host "⚠️ Drive $driveName with $(Bytes2String $total) is nearly full, $(Bytes2String $free) free"
} else {
[int64]$percent = ($used * 100) / $total
Write-Host "✅ Drive $driveName uses $percent% of $(Bytes2String $total) - $(Bytes2String $free) free"
}
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}