mirror of
https://github.com/fleschutz/PowerShell.git
synced 2024-12-04 22:01:26 +01:00
2.9 KiB
2.9 KiB
The check-drives.ps1 Script
This PowerShell script queries the free space of all drives and prints it.
Parameters
/home/markus/Repos/PowerShell/scripts/check-drives.ps1 [[-minLevel] <Int64>] [<CommonParameters>]
-minLevel <Int64>
Specifies the minimum warning level (5GB by default)
Required? false
Position? 1
Default value 5368709120
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.
Example
PS> ./check-drives.ps1
✅ Drive C: uses 489GB (53%) of 930GB, D: uses 3TB (87%) of 4TB, E: is empty
Notes
Author: Markus Fleschutz | License: CC0
Related Links
https://github.com/fleschutz/PowerShell
Script Content
<#
.SYNOPSIS
Checks the drive space
.DESCRIPTION
This PowerShell script queries the free space of all drives and prints it.
.PARAMETER minLevel
Specifies the minimum warning level (5GB by default)
.EXAMPLE
PS> ./check-drives.ps1
✅ Drive C: uses 489GB (53%) of 930GB, D: uses 3TB (87%) of 4TB, E: is empty
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([int64]$minLevel = 5GB)
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)
}
try {
Write-Progress "Querying drives..."
$drives = Get-PSDrive -PSProvider FileSystem
Write-Progress -completed "Done."
$status = "✅"
$reply = "Drive "
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)
if ($reply -ne "Drive ") { $reply += ", " }
if ($total -eq 0) {
$reply += "$name is empty"
} elseif ($free -eq 0) {
$status = "⚠️"
$reply += "$name with ($(Bytes2String $total)) is FULL"
} elseif ($free -lt $minLevel) {
$status = "⚠️"
$reply += "$name nearly full ($(Bytes2String $free) of $(Bytes2String $total) left)"
} else {
[int64]$percent = ($used * 100) / $total
$reply += "$name uses $(Bytes2String $used) ($percent%) of $(Bytes2String $total)"
}
}
Write-Host "$status $reply"
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}
(generated by convert-ps2md.ps1 as of 11/20/2024 11:51:51)