2023-07-29 10:34:04 +02:00
|
|
|
*check-drives.ps1*
|
|
|
|
================
|
2021-12-09 16:19:09 +01:00
|
|
|
|
2022-11-17 19:46:02 +01:00
|
|
|
This PowerShell script checks all drives for free space left.
|
2021-12-09 16:19:09 +01:00
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Parameters
|
|
|
|
----------
|
2021-12-09 16:19:09 +01:00
|
|
|
```powershell
|
2023-07-29 10:15:44 +02:00
|
|
|
PS> ./check-drives.ps1 [[-MinLevel] <Int32>] [<CommonParameters>]
|
2021-12-09 16:19:09 +01:00
|
|
|
|
|
|
|
-MinLevel <Int32>
|
2022-11-17 19:46:02 +01:00
|
|
|
Specifies the minimum warning level (10 GB by default)
|
2021-12-09 16:19:09 +01:00
|
|
|
|
|
|
|
Required? false
|
|
|
|
Position? 1
|
2022-11-17 19:46:02 +01:00
|
|
|
Default value 10
|
2021-12-09 16:19:09 +01:00
|
|
|
Accept pipeline input? false
|
|
|
|
Accept wildcard characters? false
|
|
|
|
|
|
|
|
[<CommonParameters>]
|
|
|
|
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
|
|
|
|
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
|
|
|
|
```
|
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Example
|
|
|
|
-------
|
2021-12-09 16:19:09 +01:00
|
|
|
```powershell
|
2023-08-06 21:36:33 +02:00
|
|
|
PS> ./check-drives.ps1
|
|
|
|
✅ Drive C: with 250GB at 10%, 225GB free
|
2021-12-09 16:19:09 +01:00
|
|
|
|
|
|
|
```
|
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Notes
|
|
|
|
-----
|
2022-11-17 19:46:02 +01:00
|
|
|
Author: Markus Fleschutz | License: CC0
|
2021-12-09 16:19:09 +01:00
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Related Links
|
|
|
|
-------------
|
2021-12-09 16:19:09 +01:00
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Script Content
|
|
|
|
--------------
|
2022-11-17 20:05:34 +01:00
|
|
|
```powershell
|
2022-11-17 20:02:26 +01:00
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Checks the drive space
|
|
|
|
.DESCRIPTION
|
|
|
|
This PowerShell script checks all drives for free space left.
|
|
|
|
.PARAMETER MinLevel
|
|
|
|
Specifies the minimum warning level (10 GB by default)
|
|
|
|
.EXAMPLE
|
2023-08-06 21:36:33 +02:00
|
|
|
PS> ./check-drives.ps1
|
|
|
|
✅ Drive C: with 250GB at 10%, 225GB free
|
2022-11-17 20:02:26 +01:00
|
|
|
.LINK
|
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
.NOTES
|
|
|
|
Author: Markus Fleschutz | License: CC0
|
|
|
|
#>
|
|
|
|
|
|
|
|
param([int]$MinLevel = 10) # 10 GB minimum
|
|
|
|
|
|
|
|
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
|
|
|
|
if ($Bytes -lt 1000) { return "$($Bytes)PB" }
|
|
|
|
$Bytes /= 1000
|
|
|
|
if ($Bytes -lt 1000) { return "$($Bytes)EB" }
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2023-05-26 12:20:18 +02:00
|
|
|
Write-Progress "⏳ Querying drives..."
|
|
|
|
$Drives = Get-PSDrive -PSProvider FileSystem
|
2023-08-06 11:42:46 +02:00
|
|
|
Write-Progress -completed "."
|
2022-11-17 20:02:26 +01:00
|
|
|
foreach($Drive in $Drives) {
|
2023-08-06 11:42:46 +02:00
|
|
|
$Details = (Get-PSDrive $Drive.Name)
|
|
|
|
if ($IsLinux) { $ID = $Drive.Name } else { $ID = $Drive.Name + ":" }
|
2022-11-17 20:02:26 +01:00
|
|
|
[int64]$Free = $Details.Free
|
|
|
|
[int64]$Used = $Details.Used
|
|
|
|
[int64]$Total = ($Used + $Free)
|
|
|
|
|
|
|
|
if ($Total -eq 0) {
|
2023-08-06 11:42:46 +02:00
|
|
|
Write-Host "✅ Drive $ID is empty"
|
2023-05-26 12:20:18 +02:00
|
|
|
} elseif ($Free -eq 0) {
|
2023-08-06 11:42:46 +02:00
|
|
|
Write-Host "⚠️ Drive $ID with $(Bytes2String $Total) is 100% full"
|
2022-11-17 20:02:26 +01:00
|
|
|
} elseif ($Free -lt $MinLevel) {
|
2023-08-06 21:36:33 +02:00
|
|
|
Write-Host "⚠️ Drive $ID with $(Bytes2String $Total) is nearly full, $(Bytes2String $Free) free"
|
2022-11-17 20:02:26 +01:00
|
|
|
} else {
|
2023-08-06 11:42:46 +02:00
|
|
|
[int]$Percent = ($Used * 100) / $Total
|
2023-08-06 21:36:33 +02:00
|
|
|
if ($Percent -gt 90) {
|
|
|
|
Write-Host "✅ Drive $ID with $(Bytes2String $Total) is $Percent% full, $(Bytes2String $Free) free"
|
|
|
|
} else {
|
|
|
|
Write-Host "✅ Drive $ID with $(Bytes2String $Total) at $Percent%, $(Bytes2String $Free) free"
|
|
|
|
}
|
2022-11-17 20:02:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
exit 0 # success
|
|
|
|
} catch {
|
|
|
|
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
|
|
|
exit 1
|
|
|
|
}
|
2022-11-17 20:05:34 +01:00
|
|
|
```
|
2022-11-17 20:02:26 +01:00
|
|
|
|
2023-08-06 21:36:33 +02:00
|
|
|
*(generated by convert-ps2md.ps1 using the comment-based help of check-drives.ps1 as of 08/06/2023 21:36:05)*
|