Archive for November, 2006

Resize-Console.ps1 – Resize console window/buffer using arrow keys


Synopsis

Perhaps resizing of Windows console is quite tricky. This script makes it easy with arrow keys.


Resize-Console.ps1

##
## Author   : Roman Kuzmin
## Synopsis : Resize console window/buffer using arrow keys
##

function Size($w, $h)
{
    New-Object System.Management.Automation.Host.Size($w, $h)
}

Write-Host '[Arrows] resize  [Esc] exit ...'
$ErrorActionPreference = 'SilentlyContinue'
for($ui = $Host.UI.RawUI;;) {
    $b = $ui.BufferSize
    $w = $ui.WindowSize
    switch($ui.ReadKey(6).VirtualKeyCode) {
        37 {
            $w = Size ($w.width - 1) $w.height
            $ui.WindowSize = $w
            $ui.BufferSize = Size $w.width $b.height
            break
        }
        39 {
            $w = Size ($w.width + 1) $w.height
            $ui.BufferSize = Size $w.width $b.height
            $ui.WindowSize = $w
            break
        }
        38 {
            $ui.WindowSize = Size $w.width ($w.height - 1)
            break
        }
        40 {
            $w = Size $w.width ($w.height + 1)
            if ($w.height -gt $b.height) {
                $ui.BufferSize = Size $b.width $w.height
            }
            $ui.WindowSize = $w
            break
        }
        27 {
            return
        }
    }
}

1 Comment

Format-Chart.ps1 – Formats output as a table with a chart column


Synopsis

The script formats output as a table with a pseudo-graphical chart column calculated for the last specified numeric property.


Examples

# Chart of process working sets
ps | Format-Chart Id, Name, WS

# Chart of file sizes in descending order
dir | sort Length -desc | Format-Chart Name, Length

Format-Chart.ps1

##
## Author   : Roman Kuzmin
## Synopsis : Formats output as a table with a chart column
## Modified : 2006.11.06
##
## -Property: properties where the last one is numeric for a chart.
## -Width: chart column width, default is 1/2 of screen buffer.
## -ForeChar: character for chart bars.
## -BackChar: character for appending bars.
## -InputObject: the objects to be formatted.
##

param
(
    [object[]]$Property = $(throw 'Supply properties'),
    [int]$Width = ($Host.UI.RawUI.BufferSize.Width/2),
    [char]$ForeChar = 9600,
    [char]$BackChar = 9617,
    [object[]]$InputObject
)

$set = $(if ($InputObject) {$InputObject} else {@($Input)}) |
Select-Object $Property

$max = ($set | Measure-Object ($Property[-1]) -Maximum).Maximum
if ($max -eq 0) {$max = 1}

$set | .{process{
    $_ | Add-Member -PassThru NoteProperty Chart (("$ForeChar"*(
    $_.$($Property[-1])/$max*$Width)).PadRight($Width, $BackChar))
}} |
Format-Table ($Property + 'Chart') -AutoSize


Leave a comment