2023-10-31 12:05:32 +01:00
|
|
|
|
<#
|
2021-07-13 21:10:02 +02:00
|
|
|
|
.SYNOPSIS
|
2024-01-24 13:03:18 +01:00
|
|
|
|
Checks the drive space
|
2021-10-04 21:29:23 +02:00
|
|
|
|
.DESCRIPTION
|
2024-01-24 13:03:18 +01:00
|
|
|
|
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)
|
2021-07-13 21:10:02 +02:00
|
|
|
|
.EXAMPLE
|
2024-01-24 13:03:18 +01:00
|
|
|
|
PS> ./check-drive-space.ps1 C
|
2024-03-13 16:52:55 +01:00
|
|
|
|
✅ Drive C: has 442GB free (56% of 1TB 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
|
|
|
|
#>
|
|
|
|
|
|
2024-01-24 13:03:18 +01:00
|
|
|
|
param([string]$driveName = "", [int64]$minLevel = 10 * 1000 * 1000) # GB
|
2021-02-26 11:06:01 +01:00
|
|
|
|
|
2024-01-24 13:03:18 +01:00
|
|
|
|
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"
|
|
|
|
|
}
|
2021-08-24 20:46:03 +02:00
|
|
|
|
|
2024-01-24 13:03:18 +01:00
|
|
|
|
try {
|
|
|
|
|
if ($driveName -eq "" ) { $driveName = Read-Host "Enter the drive name to check" }
|
2021-02-26 11:06:01 +01:00
|
|
|
|
|
2024-01-24 13:03:18 +01:00
|
|
|
|
$details = (Get-PSDrive $driveName)
|
|
|
|
|
if (-not $IsLinux) { $driveName = $driveName + ":" }
|
|
|
|
|
[int64]$free = $details.Free
|
|
|
|
|
[int64]$used = $details.Used
|
|
|
|
|
[int64]$total = ($used + $free)
|
2021-12-06 17:00:49 +01:00
|
|
|
|
|
2024-01-24 13:03:18 +01:00
|
|
|
|
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 {
|
2024-03-16 11:36:02 +01:00
|
|
|
|
[int64]$percent = ($used * 100) / $total
|
2024-03-13 16:52:55 +01:00
|
|
|
|
Write-Host "✅ Drive $driveName has $(Bytes2String $free) free ($percent% of $(Bytes2String $total) used)"
|
2024-01-24 13:03:18 +01:00
|
|
|
|
}
|
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
|
|
|
|
|
}
|