PowerShell/docs/check-drives.md

109 lines
2.9 KiB
Markdown
Raw Normal View History

2023-07-29 10:34:04 +02:00
*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>
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
2023-10-19 08:12:00 +02:00
✅ Drive C: uses 49%, 512GB free of 1TB
✅ Drive D: uses 84%, 641GB free of 4TB
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
2022-11-17 20:02:26 +01:00
Specifies the minimum warning level (10 GB by default)
.EXAMPLE
2023-08-06 21:36:33 +02:00
PS> ./check-drives.ps1
2023-10-19 08:12:00 +02:00
✅ Drive C: uses 49%, 512GB free of 1TB
✅ Drive D: uses 84%, 641GB free of 4TB
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
2023-09-01 17:53:03 +02: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"
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
$minLevel *= 1000 * 1000 * 1000
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 {
2023-09-01 17:53:03 +02:00
[int]$percent = ($used * 100) / $total
2023-10-19 08:12:00 +02:00
Write-Host "✅ Drive $name uses $percent%, $(Bytes2String $free) free of $(Bytes2String $total)"
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-12-16 10:12:58 +01:00
*(generated by convert-ps2md.ps1 using the comment-based help of check-drives.ps1 as of 12/16/2023 10:12:18)*