PowerShell/Scripts/check-drive-space.ps1

34 lines
946 B
PowerShell
Raw Normal View History

2021-04-21 19:53:52 +02:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
check-drive-space.ps1 [<drive>] [<min-level>]
.DESCRIPTION
2021-08-29 17:50:03 +02:00
Checks the given drive for free space left.
2021-07-13 21:10:02 +02:00
.EXAMPLE
PS> .\check-drive-space.ps1 C
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
2021-08-29 17:50:03 +02:00
Author: Markus Fleschutz · License: CC0
2021-02-26 11:06:01 +01:00
#>
2021-07-15 15:51:22 +02:00
param([string]$Drive = "", [int]$MinLevel = 20) # minimum level in GB
2021-02-26 11:06:01 +01:00
try {
2021-08-24 20:46:03 +02:00
if ($Drive -eq "" ) { $Drive = read-host "Enter drive to check" }
$DriveDetails = (get-psdrive $Drive)
2021-03-20 15:51:03 +01:00
[int]$Free = (($DriveDetails.Free / 1024) / 1024) / 1024
[int]$Used = (($DriveDetails.Used / 1024) / 1024) / 1024
[int]$Total = ($Used + $Free)
2021-02-26 11:06:01 +01:00
2021-03-20 15:51:03 +01:00
if ($Free -lt $MinLevel) {
write-warning "Drive $Drive has only $Free GB left to use! ($Used GB out of $Total GB in use, minimum is $MinLevel GB)"
2021-02-27 10:50:58 +01:00
exit 1
}
2021-04-17 18:54:45 +02:00
"✔️ $Free GB left on drive $Drive ($Used GB of $Total GB used)"
2021-02-27 10:50:58 +01:00
exit 0
2021-02-26 11:06:01 +01:00
} catch {
2021-05-02 21:30:48 +02:00
write-error "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2021-02-26 11:06:01 +01:00
exit 1
}