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-09-01 17:53:03 +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
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-09-01 17:53:03 +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
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-05-26 12:20:18 +02:00
Write-Progress "⏳ Querying drives..."
2023-09-01 17:53:03 +02:00
$drives = Get-PSDrive -PSProvider FileSystem
$minLevel *= 1000 * 1000 * 1000
2023-08-06 11:42:46 +02: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
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
2023-09-20 17:05:11 +02:00
*(generated by convert-ps2md.ps1 using the comment-based help of check-drives.ps1 as of 09/20/2023 17:04:37)*