How to create colored and adjustable Percentage Bar in the PowerShell

Cover

In one of the scripts on GitHub I found interesting function Show-PercentageGraph, written by vmdude (Frederic MARTIN).

I decided on basis of this function to develop my own more robust and adjustable function.

I introduce to you the function New-PercentageBar that is part of MS-Module from my PowerShell repository.

New-PercentageBar

One of the powerful features of my function is ability not only «draw» the percentage graph on the PowerShell console by Write-Host cmdlet, but return percentage bar value as graph string!

New-PercentageBar 75 –DrawBar
New-PercentageBar -Percent 75

What are use-cases?

  • First, you can create calculated property which will contain the percentage bar and add it to your objects.

  • Second, you can export these objects to a file (text or Excel). I will show you a lot of examples in the continuation of the article.

Parameters

Let’s start with the function parameters.

  • -BarView and -Length parameters control how the bar will look like. The -BarView parameter supports Intellisense.
New-PercentageBar 80 -BarLength 50
New-PercentageBar 80 -Length 100
New-PercentageBar 80 -BarView SimpleThin
New-PercentageBar 80 -BarView SimpleThick2
New-PercentageBar 80 -BarView AdvancedThick
New-PercentageBar 80 -BarView AdvancedThin2

01.New-PercentageBar_BarView

  • Note that maximum possible precision of the bar is 1% while –Length parameter is set to its maximum value (100 characters). The minimum precision is 10% (one bar character represents ten or less percent) while –Length equal to 10. The default –Length is 20, this value is suitable in most cases.

02.New-PercentageBar_BarView

03.New-PercentageBar_BarView

  • -GreenBorder and -YellowBorder control at which percent the color changes (from green to yellow/from yellow to red) will occur.
New-PercentageBar 100 -DrawBar -BarView AdvancedThin2 -GreenBorder 80 -YellowBorder 90
New-PercentageBar 100 -DrawBar -BarView AdvancedThin2 -GreenBorder 50 -YellowBorder 80
  • -NoPercent is a switch parameter that controls to show or not the percent number.
New-PercentageBar 60
New-PercentageBar 60 -NoPercent

04.New-PercentageBar_BorderNoPercent

  • The percent value you can pass to the function by two different ways. The first by -Percent parameter and the second by -Value/-MaxValue parameters pair, where -Value is the current value and -MaxValue is the 100% value.

  • The -Percent parameter supports pipeline.

New-PercentageBar 90
90 | New-PercentageBar

05.New-PercentageBar_ParameterSets

Use cases: Get disk usage

One more example. Get disk usage by Get-WmiObject.

$VolumeC = gwmi Win32_LogicalDisk | ? {$_.DeviceID -eq 'c:'}
New-PercentageBar -Value ($VolumeC.Size-$VolumeC.Freespace) -MaxValue $VolumeC.Size -DrawBar

06.New-PercentageBar_Get-WmiObject

Draw percentage bar

  • Last and the best example with –DrawBar parameter.

  • Michal Millar wrote an awesome function named Get-MOTD (Get Message Of The Day) that shows excellent computer resources report.

  • I added minor changes to the function and embedded my New-PercentageBar function into the Get-MOTD.ps1 script. This is consolidated result.

Get-MOTD.ps1

07.New-PercentageBar_Get-MOTD

  • From the VMware PowerCLi console.

08.New-PercentageBar_Get-MOTD

Use cases: Add calculated properties

  • With this technique you can add calculated property to any object.

  • In the next example I will add SizeBar property to the objects, returned by Get-ChildItem. This property will contain percentage bar that represents each file size compared to the whole parent folder size.

$Folder = "$env:USERPROFILE\Documents\"
$FolderSize = (Get-ChildItem -Path $Folder |measure -Property Length -Sum).Sum
Get-ChildItem -Path $Folder -File | sort Length -Descending | select -First 10 | select Name, Length, @{N='SizeBar'; E={New-PercentageBar -Value $_.Length -MaxValue $FolderSize}} | Format-Table -AutoSize

09.New-PercentageBar_Get-ChildItem

  • If you omit the –DrawBar parameter, the New-PercentageBar will return you string that contains the bar.

  • You can pass the output to any Format- cmdlet (ft, fl, fc) to change the object representation.

Get-Alias -Definition format-*
Get-ChildItem -Path $Folder -File | sort Length -Descending | select -First 10 | select Name, Length, @{N='SizeBar'; E={New-PercentageBar -Value $_.Length -MaxValue $FolderSize}} | fl

Use cases: VMware Datastore usage

  • Very last example for VMware administrators. Datastore usage report. Here I create a new property Usage% that represents Datastore usage.
Connect-VIServer VC1 –wa SilentlyContinue
Get-Datastore | select Name, Type, @{N=’ CapacityGB’; E={[Math]::Round($_.CapacityGB, 2)}}, @{N=’FreeSpaceGB’; E={[Math]::Round($_.FreeSpaceGB, 2)}}, @{N=’Usage%’; E={New-PercentageBar –Value ($_.CapacityGB-$_.FreeSpaceGB) –MaxValue $_.CapacityGB}} | sort ‘Usage%’ –Descending |ft -au

10.New-PercentageBar_Get-Datastore

  • If you will replace the last cmdlet in the pipeline ft –au with Export-Csv you will be able to export your objects including the Usage% property.

  • Note, you can sort the report on this property by | sort ‘Usage%’ -Descending.

Get-Datastore | select Name, Type, @{N=’ CapacityGB’; E={[Math]::Round($_.CapacityGB, 2)}}, @{N=’FreeSpaceGB’; E={[Math]::Round($_.FreeSpaceGB, 2)}}, @{N=’Usage%’; E={New-PercentageBar –Value ($_.CapacityGB-$_.FreeSpaceGB) –MaxValue $_.CapacityGB}} | sort ‘Usage%’ –Descending | epcsv –notype –Encoding utf8 .\Datastore.csv

11.New-PercentageBar_Export-Csv

Encoding

  • There is one requirement only for successful export.

  • You must use UTF8 encoding for correct characters’ view with Export-Csv and Out-File cmdlets!

1..100 |% {[PSCustomObject]@{'Num'=$_; 'Bar'=(New-PercentageBar $_)}} | Export-Csv .\Report.csv -Encoding utf8 -notype
New-PercentageBar 90 | Out-File .\Report.txt -Encoding utf8
10..100 | % {New-PercentageBar $_ | Out-File .\Report.txt -Encoding utf8 -Append}

12.New-PercentageBar_Out-File

You may also like:

Write-Menu – Create interactive dynamic Menu in PowerShell
Start-SleepProgress – Put PowerShell scripts to sleep with Progress Bar

4 thoughts on “How to create colored and adjustable Percentage Bar in the PowerShell

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s