PowerShell/scripts/check-drive-space.ps1

40 lines
1.1 KiB
PowerShell
Raw Normal View History

2023-10-31 12:05:32 +01:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
2022-01-29 12:47:46 +01:00
Checks a drive for free space left
2021-10-04 21:29:23 +02:00
.DESCRIPTION
2022-01-29 12:47:46 +01:00
This PowerShell script checks a drive for free space left (20 GB by default).
2021-10-12 21:51:51 +02:00
.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
2022-09-06 21:42:04 +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-12-06 17:00:49 +01:00
2022-11-18 16:54:04 +01:00
& "$PSScriptRoot/speak-english.ps1" "Drive $Drive has $Free GB left ($Total GB total)"
2021-09-27 10:09:45 +02:00
exit 0 # success
2021-02-26 11:06:01 +01:00
} catch {
2022-04-13 12:06:32 +02:00
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2021-02-26 11:06:01 +01:00
exit 1
}