mirror of
https://github.com/fleschutz/PowerShell.git
synced 2024-11-22 07:53:21 +01:00
3.0 KiB
3.0 KiB
check-drives.ps1
This PowerShell script checks all drives for free space left.
Parameters
PS> ./check-drives.ps1 [[-MinLevel] <Int32>] [<CommonParameters>]
-MinLevel <Int32>
Specifies the minimum warning level (10 GB by default)
Required? false
Position? 1
Default value 10
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: with 250GB at 10%, 225GB free
Notes
Author: Markus Fleschutz | License: CC0
Related Links
https://github.com/fleschutz/PowerShell
Script Content
<#
.SYNOPSIS
Checks the drive space
.DESCRIPTION
This PowerShell script checks all drives for free space left.
.PARAMETER MinLevel
Specifies the minimum warning level (10 GB by default)
.EXAMPLE
PS> ./check-drives.ps1
✅ Drive C: with 250GB at 10%, 225GB free
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([int]$MinLevel = 10) # 10 GB minimum
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
if ($Bytes -lt 1000) { return "$($Bytes)PB" }
$Bytes /= 1000
if ($Bytes -lt 1000) { return "$($Bytes)EB" }
}
try {
Write-Progress "⏳ Querying drives..."
$Drives = Get-PSDrive -PSProvider FileSystem
Write-Progress -completed "."
foreach($Drive in $Drives) {
$Details = (Get-PSDrive $Drive.Name)
if ($IsLinux) { $ID = $Drive.Name } else { $ID = $Drive.Name + ":" }
[int64]$Free = $Details.Free
[int64]$Used = $Details.Used
[int64]$Total = ($Used + $Free)
if ($Total -eq 0) {
Write-Host "✅ Drive $ID is empty"
} elseif ($Free -eq 0) {
Write-Host "⚠️ Drive $ID with $(Bytes2String $Total) is 100% full"
} elseif ($Free -lt $MinLevel) {
Write-Host "⚠️ Drive $ID with $(Bytes2String $Total) is nearly full, $(Bytes2String $Free) free"
} else {
[int]$Percent = ($Used * 100) / $Total
if ($Percent -gt 90) {
Write-Host "✅ Drive $ID with $(Bytes2String $Total) is $Percent% full, $(Bytes2String $Free) free"
} else {
Write-Host "✅ Drive $ID with $(Bytes2String $Total) at $Percent%, $(Bytes2String $Free) free"
}
}
}
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}
(generated by convert-ps2md.ps1 using the comment-based help of check-drives.ps1 as of 08/06/2023 21:36:05)