PowerShell/docs/check-drives.md

106 lines
2.9 KiB
Markdown
Raw Normal View History

2024-11-08 12:38:20 +01:00
The *check-drives.ps1* Script
===========================
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
2024-11-08 12:35:11 +01:00
/home/markus/Repos/PowerShell/scripts/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-11-08 12:35:11 +01:00
Specifies the minimum warning level (5GB by default)
2021-12-09 16:19:09 +01:00
Required? false
Position? 1
2024-11-08 12:35:11 +01:00
Default value 5368709120
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-11-08 12:35:11 +01:00
✅ Drive C: uses 489GB (53%) of 930GB, D: uses 3TB (87%) of 4TB, E: is empty
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-11-08 12:35:11 +01:00
Specifies the minimum warning level (5GB by default)
2022-11-17 20:02:26 +01:00
.EXAMPLE
2023-08-06 21:36:33 +02:00
PS> ./check-drives.ps1
2024-11-08 12:35:11 +01:00
✅ Drive C: uses 489GB (53%) of 930GB, D: uses 3TB (87%) of 4TB, E: is empty
2022-11-17 20:02:26 +01:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
2024-11-08 12:35:11 +01:00
param([int64]$minLevel = 5GB)
2022-11-17 20:02:26 +01:00
2024-11-08 12:35:11 +01:00
function Bytes2String { param([int64]$bytes)
if ($bytes -lt 1KB) { return "$bytes bytes" }
if ($bytes -lt 1MB) { return '{0:N0}KB' -f ($bytes / 1KB) }
if ($bytes -lt 1GB) { return '{0:N0}MB' -f ($bytes / 1MB) }
if ($bytes -lt 1TB) { return '{0:N0}GB' -f ($bytes / 1GB) }
if ($bytes -lt 1PB) { return '{0:N0}TB' -f ($bytes / 1TB) }
return '{0:N0}GB' -f ($bytes / 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-11-08 12:35:11 +01:00
Write-Progress -completed "Done."
$status = "✅"
$reply = "Drive "
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)
2024-11-08 12:35:11 +01:00
if ($reply -ne "Drive ") { $reply += ", " }
2023-09-01 17:53:03 +02:00
if ($total -eq 0) {
2024-11-08 12:35:11 +01:00
$reply += "$name is empty"
2023-09-01 17:53:03 +02:00
} elseif ($free -eq 0) {
2024-11-08 12:35:11 +01:00
$status = "⚠️"
$reply += "$name with ($(Bytes2String $total)) is FULL"
2023-09-01 17:53:03 +02:00
} elseif ($free -lt $minLevel) {
2024-11-08 12:35:11 +01:00
$status = "⚠️"
$reply += "$name nearly full ($(Bytes2String $free) of $(Bytes2String $total) left)"
2022-11-17 20:02:26 +01:00
} else {
2024-03-27 17:36:59 +01:00
[int64]$percent = ($used * 100) / $total
2024-11-08 12:35:11 +01:00
$reply += "$name uses $(Bytes2String $used) ($percent%) of $(Bytes2String $total)"
2022-11-17 20:02:26 +01:00
}
}
2024-11-08 12:35:11 +01:00
Write-Host "$status $reply"
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-11-20 11:52:20 +01:00
*(generated by convert-ps2md.ps1 as of 11/20/2024 11:51:51)*