PowerShell/docs/check-drive-space.md

114 lines
3.3 KiB
Markdown
Raw Normal View History

2024-01-25 13:37:12 +01:00
Script: *check-drive-space.ps1*
========================
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-01-25 13:31:10 +01:00
PS> ./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-03-27 17:36:59 +01:00
✅ Drive C: has 442GB free (56% of 1TB used)
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-03-27 17:36:59 +01:00
✅ Drive C: has 442GB free (56% of 1TB used)
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
Write-Host "✅ Drive $driveName has $(Bytes2String $free) free ($percent% of $(Bytes2String $total) used)"
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-03-27 17:36:59 +01:00
*(generated by convert-ps2md.ps1 using the comment-based help of check-drive-space.ps1 as of 03/27/2024 17:36:24)*