PowerShell/Scripts/check-drive-space.ps1

34 lines
958 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-09-08 15:17:27 +02:00
Checks a drive for free space left (20 GB by default)
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) {
2021-09-11 11:20:27 +02:00
write-warning "Drive $Drive has only $Free GB left to use! ($Used of $Total GB used, minimum is $MinLevel GB)"
2021-02-27 10:50:58 +01:00
exit 1
}
2021-09-11 11:20:27 +02:00
"✔️ $Free GB left on drive $Drive ($Used of $Total GB used)"
2021-02-27 10:50:58 +01:00
exit 0
2021-02-26 11:06:01 +01:00
} catch {
2021-09-16 20:19:10 +02:00
"⚠️ Error: $($Error[0]) ($($MyInvocation.MyCommand.Name):$($_.InvocationInfo.ScriptLineNumber))"
2021-02-26 11:06:01 +01:00
exit 1
}