mirror of
https://github.com/fleschutz/PowerShell.git
synced 2024-11-25 01:14:16 +01:00
114 lines
3.3 KiB
Markdown
114 lines
3.3 KiB
Markdown
Script: *check-drive-space.ps1*
|
|
========================
|
|
|
|
This PowerShell script checks the given drive for free space left (10 GB by default).
|
|
|
|
Parameters
|
|
----------
|
|
```powershell
|
|
/home/markus/Repos/PowerShell/scripts/check-drive-space.ps1 [[-driveName] <String>] [[-minLevel] <Int64>] [<CommonParameters>]
|
|
|
|
-driveName <String>
|
|
Specifies the drive name to check (e.g. "C")
|
|
|
|
Required? false
|
|
Position? 1
|
|
Default value
|
|
Accept pipeline input? false
|
|
Accept wildcard characters? false
|
|
|
|
-minLevel <Int64>
|
|
Specifies the minimum level in bytes (10GB by default)
|
|
|
|
Required? false
|
|
Position? 2
|
|
Default value 10000000
|
|
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
|
|
-------
|
|
```powershell
|
|
PS> ./check-drive-space.ps1 C
|
|
✅ Drive C: uses 56% of 1TB - 442GB free
|
|
|
|
```
|
|
|
|
Notes
|
|
-----
|
|
Author: Markus Fleschutz | License: CC0
|
|
|
|
Related Links
|
|
-------------
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
Script Content
|
|
--------------
|
|
```powershell
|
|
<#
|
|
.SYNOPSIS
|
|
Checks the drive space
|
|
.DESCRIPTION
|
|
This PowerShell script checks the given drive for free space left (10 GB by default).
|
|
.PARAMETER driveName
|
|
Specifies the drive name to check (e.g. "C")
|
|
.PARAMETER minLevel
|
|
Specifies the minimum level in bytes (10GB by default)
|
|
.EXAMPLE
|
|
PS> ./check-drive-space.ps1 C
|
|
✅ Drive C: uses 56% of 1TB - 442GB free
|
|
.LINK
|
|
https://github.com/fleschutz/PowerShell
|
|
.NOTES
|
|
Author: Markus Fleschutz | License: CC0
|
|
#>
|
|
|
|
param([string]$driveName = "", [int64]$minLevel = 10 * 1000 * 1000) # GB
|
|
|
|
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"
|
|
}
|
|
|
|
try {
|
|
if ($driveName -eq "" ) { $driveName = Read-Host "Enter the drive name to check" }
|
|
|
|
$details = (Get-PSDrive $driveName)
|
|
if (-not $IsLinux) { $driveName = $driveName + ":" }
|
|
[int64]$free = $details.Free
|
|
[int64]$used = $details.Used
|
|
[int64]$total = ($used + $free)
|
|
|
|
if ($total -eq 0) {
|
|
Write-Host "✅ Drive $driveName is empty"
|
|
} elseif ($free -eq 0) {
|
|
Write-Host "⚠️ Drive $driveName with $(Bytes2String $total) is full"
|
|
} elseif ($free -lt $minLevel) {
|
|
Write-Host "⚠️ Drive $driveName with $(Bytes2String $total) is nearly full, $(Bytes2String $free) free"
|
|
} else {
|
|
[int64]$percent = ($used * 100) / $total
|
|
Write-Host "✅ Drive $driveName uses $percent% of $(Bytes2String $total) - $(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-drive-space.ps1 as of 11/08/2024 12:34:46)*
|