-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathShow-PSDriveUsage.ps1
84 lines (75 loc) · 3.26 KB
/
Show-PSDriveUsage.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<#
.SYNOPSIS
Displays drive usage graphically, and with a human-readable summary.
.FUNCTIONALITY
Files
.INPUTS
An object with a Name property that corresponds to a drive name.
.LINK
Import-CharConstants.ps1
.LINK
Format-ByteUnits.ps1
.LINK
Write-Info.ps1
.EXAMPLE
Show-PSDriveUsage.ps1 C -AsAscii
#################_______________________________________________________________________
C:\ Windows [NTFS] 953GB = 762GB (79%) free + 191GB (20%) used
.EXAMPLE
Show-PSDriveUsage.ps1 /home -AsAscii
###################_____________________________________________________________________
/home [ext3] 4TB = 3TB (73%) free + 792GB (21%) used
#>
#Requires -Version 7
[CmdletBinding()] Param(
# A drive name to display the usage for. All ready fixed drives are displayed if none is specified.
[Parameter(Position=0,ValueFromPipelineByPropertyName=$true,ValueFromRemainingArguments=$true)][string[]] $Name = @(),
# Display the graph as ASCII.
[switch] $AsAscii
)
Begin
{
if($AsAscii) {$usedchar, $freechar = '#', '_'}
else {Import-CharConstants.ps1 'full block' 'light shade'; $usedchar, $freechar = ${full block}, ${light shade}}
filter Show-DriveUsage
{
[CmdletBinding()] Param(
[Parameter(ValueFromPipelineByPropertyName=$true)][string] $Name,
[Parameter(ValueFromPipelineByPropertyName=$true)][string] $VolumeLabel,
[Parameter(ValueFromPipelineByPropertyName=$true)][string] $DriveFormat,
[Parameter(ValueFromPipelineByPropertyName=$true)][IO.DirectoryInfo] $RootDirectory,
[Parameter(ValueFromPipelineByPropertyName=$true)][long] $TotalSize,
[Parameter(ValueFromPipelineByPropertyName=$true)][long] $TotalFreeSpace,
[Parameter(ValueFromPipelineByPropertyName=$true)][long] $AvailableFreeSpace
)
$blocksize, $usedspace = [long]::DivRem($TotalSize, [Console]::WindowWidth).Item1, ($TotalSize - $TotalFreeSpace)
$usedpercent, $freepercent, $size, $free, $used, $usedgraph, $freegraph =
([long]::DivRem(100*$usedspace, $TotalSize).Item1),
([long]::DivRem(100*$AvailableFreeSpace, $TotalSize).Item1),
($TotalSize |Format-ByteUnits.ps1 -Precision 0),
($AvailableFreeSpace |Format-ByteUnits.ps1 -Precision 0),
($usedspace |Format-ByteUnits.ps1 -Precision 0),
(New-Object string ($usedchar, [long]::DivRem($usedspace, $blocksize).Item1)),
(New-Object string ($freechar, [long]::DivRem($AvailableFreeSpace, $blocksize).Item1))
$color = switch($freepercent)
{
{$_ -lt 10} {'Red'}
{$_ -lt 20} {'Yellow'}
default {'Green'}
}
Write-Info.ps1 $usedgraph -fg Cyan -NoNewLine
Write-Info.ps1 $freegraph -fg DarkCyan
$fqdn = $Name -ne $VolumeLabel ? "$Name $VolumeLabel [$DriveFormat]" : "$Name [$DriveFormat]"
Write-Info.ps1 "$fqdn $size = $free ($freepercent%) free + $used ($usedpercent%) used" -fg $color
}
}
Process
{
if($Name) {$Name |ForEach-Object {[IO.DriveInfo] $_} |Show-DriveUsage}
else
{
[IO.DriveInfo]::GetDrives() |
Where-Object {$_.DriveType -eq 'Fixed' -and $_.IsReady -and $_.TotalSize -gt 0} |
Show-DriveUsage
}
}