PowerShell/Scripts/check-drive-space.ps1

39 lines
1.1 KiB
PowerShell
Raw Normal View History

2021-09-27 10:38:12 +02:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
2021-09-08 15:17:27 +02:00
Checks a drive for free space left (20 GB by default)
2021-10-04 21:29:23 +02:00
.DESCRIPTION
2021-10-12 21:51:51 +02:00
This script checks a drive for free space left (20 GB by default).
.PARAMETER Drive
Specifies the drive to check
.PARAMETER MinLevel
Specifies the minimum level in Gigabyte
2021-07-13 21:10:02 +02:00
.EXAMPLE
2021-09-24 17:19:49 +02:00
PS> ./check-drive-space C
2021-10-04 17:42:06 +02:00
172 GB left on drive C (61 of 233 GB used)
2021-07-13 21:10:02 +02:00
.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
}
"✔️ Drive $Drive has $Free GB left ($Used of $Total GB used)"
2021-09-27 10:09:45 +02:00
exit 0 # success
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
}