2024-01-25 13:37:12 +01:00
|
|
|
Script: *check-drives.ps1*
|
|
|
|
========================
|
2021-12-09 16:19:09 +01:00
|
|
|
|
2023-09-01 17:53:03 +02:00
|
|
|
This PowerShell script queries the free space of all drives and prints it.
|
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-09-01 17:53:03 +02:00
|
|
|
PS> ./check-drives.ps1 [[-minLevel] <Int64>] [<CommonParameters>]
|
2021-12-09 16:19:09 +01:00
|
|
|
|
2023-09-01 17:53:03 +02:00
|
|
|
-minLevel <Int64>
|
2024-03-27 17:36:59 +01:00
|
|
|
Specifies the minimum warning level (10GB 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
|
2024-08-15 09:51:46 +02:00
|
|
|
✅ Drive C: uses 49% of 1TB - 512GB free
|
|
|
|
✅ Drive D: uses 84% of 4TB - 641GB 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
|
2023-09-01 17:53:03 +02:00
|
|
|
This PowerShell script queries the free space of all drives and prints it.
|
|
|
|
.PARAMETER minLevel
|
2024-03-27 17:36:59 +01:00
|
|
|
Specifies the minimum warning level (10GB by default)
|
2022-11-17 20:02:26 +01:00
|
|
|
.EXAMPLE
|
2023-08-06 21:36:33 +02:00
|
|
|
PS> ./check-drives.ps1
|
2024-08-15 09:51:46 +02:00
|
|
|
✅ Drive C: uses 49% of 1TB - 512GB free
|
|
|
|
✅ Drive D: uses 84% of 4TB - 641GB free
|
2022-11-17 20:02:26 +01:00
|
|
|
.LINK
|
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
.NOTES
|
|
|
|
Author: Markus Fleschutz | License: CC0
|
|
|
|
#>
|
|
|
|
|
2023-09-01 17:53:03 +02:00
|
|
|
param([int64]$minLevel = 10) # 10 GB minimum
|
2022-11-17 20:02:26 +01:00
|
|
|
|
2024-03-27 17:36:59 +01:00
|
|
|
function Bytes2String { param([int64]$number)
|
|
|
|
if ($number -lt 1KB) { return "$number bytes" }
|
|
|
|
if ($number -lt 1MB) { return '{0:N0}KB' -f ($number / 1KB) }
|
|
|
|
if ($number -lt 1GB) { return '{0:N0}MB' -f ($number / 1MB) }
|
|
|
|
if ($number -lt 1TB) { return '{0:N0}GB' -f ($number / 1GB) }
|
|
|
|
if ($number -lt 1PB) { return '{0:N0}TB' -f ($number / 1TB) }
|
|
|
|
return '{0:N0}GB' -f ($number / 1PB)
|
2022-11-17 20:02:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2023-12-07 20:24:45 +01:00
|
|
|
Write-Progress "Querying drives..."
|
2023-09-01 17:53:03 +02:00
|
|
|
$drives = Get-PSDrive -PSProvider FileSystem
|
2024-03-27 17:36:59 +01:00
|
|
|
$minLevel *= 1GB
|
2023-12-07 20:24:45 +01:00
|
|
|
Write-Progress -completed " "
|
2023-09-01 17:53:03 +02:00
|
|
|
foreach($drive in $drives) {
|
|
|
|
$details = (Get-PSDrive $drive.Name)
|
|
|
|
if ($IsLinux) { $name = $drive.Name } else { $name = $drive.Name + ":" }
|
|
|
|
[int64]$free = $details.Free
|
|
|
|
[int64]$used = $details.Used
|
|
|
|
[int64]$total = ($used + $free)
|
2022-11-17 20:02:26 +01:00
|
|
|
|
2023-09-01 17:53:03 +02:00
|
|
|
if ($total -eq 0) {
|
|
|
|
Write-Host "✅ Drive $name is empty"
|
|
|
|
} elseif ($free -eq 0) {
|
|
|
|
Write-Host "⚠️ Drive $name with $(Bytes2String $total) is full"
|
|
|
|
} elseif ($free -lt $minLevel) {
|
|
|
|
Write-Host "⚠️ Drive $name with $(Bytes2String $total) is nearly full, $(Bytes2String $free) free"
|
2022-11-17 20:02:26 +01:00
|
|
|
} else {
|
2024-03-27 17:36:59 +01:00
|
|
|
[int64]$percent = ($used * 100) / $total
|
2024-08-15 09:51:46 +02:00
|
|
|
Write-Host "✅ Drive $name uses $percent% of $(Bytes2String $total) - $(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
|
|
|
|
2024-08-15 09:51:46 +02:00
|
|
|
*(generated by convert-ps2md.ps1 using the comment-based help of check-drives.ps1 as of 08/15/2024 09:50:45)*
|