PowerShell/docs/check-drive-space.md

114 lines
3.3 KiB
Markdown
Raw Permalink Normal View History

2024-11-08 12:38:20 +01:00
The *check-drive-space.ps1* Script
===========================
2021-11-08 21:36:42 +01:00
2024-01-25 13:31:10 +01:00
This PowerShell script checks the given drive for free space left (10 GB by default).
2021-11-08 21:36:42 +01:00
2023-07-29 10:04:38 +02:00
Parameters
----------
2021-11-08 21:36:42 +01:00
```powershell
2024-11-08 12:35:11 +01:00
/home/markus/Repos/PowerShell/scripts/check-drive-space.ps1 [[-driveName] <String>] [[-minLevel] <Int64>] [<CommonParameters>]
2021-11-08 21:36:42 +01:00
2024-01-25 13:31:10 +01:00
-driveName <String>
Specifies the drive name to check (e.g. "C")
2021-11-08 21:36:42 +01:00
Required? false
Position? 1
Default value
Accept pipeline input? false
Accept wildcard characters? false
2024-01-25 13:31:10 +01:00
-minLevel <Int64>
Specifies the minimum level in bytes (10GB by default)
2021-11-08 21:36:42 +01:00
Required? false
Position? 2
2024-01-25 13:31:10 +01:00
Default value 10000000
2021-11-08 21:36:42 +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-11-08 21:36:42 +01:00
```powershell
2024-01-25 13:31:10 +01:00
PS> ./check-drive-space.ps1 C
2024-08-15 09:51:46 +02:00
✅ Drive C: uses 56% of 1TB - 442GB free
2021-11-08 21:36:42 +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-11-08 21:36:42 +01:00
2023-07-29 10:04:38 +02:00
Related Links
-------------
2021-11-08 21:36:42 +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
2024-01-25 13:31:10 +01:00
Checks the drive space
2022-11-17 20:02:26 +01:00
.DESCRIPTION
2024-01-25 13:31:10 +01:00
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)
2022-11-17 20:02:26 +01:00
.EXAMPLE
2024-01-25 13:31:10 +01:00
PS> ./check-drive-space.ps1 C
2024-08-15 09:51:46 +02:00
✅ Drive C: uses 56% of 1TB - 442GB free
2022-11-17 20:02:26 +01:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
2024-01-25 13:31:10 +01:00
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"
}
2022-11-17 20:02:26 +01:00
try {
2024-01-25 13:31:10 +01:00
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 {
2024-03-27 17:36:59 +01:00
[int64]$percent = ($used * 100) / $total
2024-08-15 09:51:46 +02:00
Write-Host "✅ Drive $driveName uses $percent% of $(Bytes2String $total) - $(Bytes2String $free) free"
2024-01-25 13:31:10 +01:00
}
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)*