Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write-Host into columns (formatted output)

Tags:

powershell

I am trying to use Write-Host to output data into columns. I have found a way to do this, but it is quite messy:

Firstly, I set the console size using the following function:

function fn_SetConsoleSize {
    param ([int]$height, [int]$width, [string]$title)

    $bheight = $height
    if ($height -le 50) { 
        $bHeight = 51
    }

    $bwidth = $width
    if ($width -le 150) { 
        $bwidth = 150
    }

    $pshost = Get-Host
    $pswindow = $pshost.ui.rawui
    $newsize = $pswindow.buffersize
    $newsize.height = $bHeight
    $newsize.width = $bwidth
    $pswindow.buffersize = $newsize

    <# Window Size #>
    $newsize = $pswindow.windowsize
    $newsize.height = $height
    $newsize.width = $width
    $pswindow.windowsize = $newsize

    <# Other Console Changes #>
    $pswindow.windowtitle = $title
    $pswindow.foregroundcolor = "Yellow"
    $pswindow.backgroundcolor = "Black"
}

Then I just set the column sizes using whitespaces:

Write-Host ( " " * 20 ) "Candidates        = $($Counts[0])" -nonewline
Write-Host (" " * ( 32 - $($Counts[0]).tostring().length)) "Candidates        = $($Counts[0])"

Write-Host ( " " * 20 ) "Contacts          = $($Counts[1])" -nonewline
Write-Host (" " * ( 32 - $($Counts[1]).tostring().length))  "Contacts          = $($Counts[1])"

This does output how I want it to, but this part of my project is going to be quite long, so I would like to simplify this quite a lot if possible.

Here is an example of the output:

Enter image description here

like image 387
Owain Esau Avatar asked Mar 23 '26 01:03

Owain Esau


1 Answers

StephenP is on point for the direction he's giving you.

As for this...

it does help but I wouldn't be able to keep the colours using here-strings, but that's not really an issue. Out of interest, why would you avoid using Write-Host?

Using Write-Host has been a hotly debated topic for a long while. Even by the inventor / author of PowerShell.

Write-Host Considered Harmful - by PowerShell founder Jeffrey Snover

http://www.jsnover.com/blog/2013/12/07/write-host-considered-harmful

When you are writing or reviewing PowerShell scripts, I’d like you to remember the following rule of thumb:

◾Using Write-Host is almost always wrong.

Write-Host is almost always the wrong thing to do because it interferes with automation. There are typically two reasons why people use Write-Host:

Lot's of other articles exist on the topic. In earlier versions of PoSH, Write-Host could not be used in pipeline, as the moment yo use it the data is gone from the buffer.

However, in PoSHv5 Jeffrey Snover now says...

With PowerShell v5 Write-Host no longer "kills puppies". data is captured into info stream https://learn.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Write-Information?view=powershell-5.1

Description

The Write-Information cmdlet specifies how Windows PowerShell handles information stream data for a command.

Windows PowerShell 5.0 introduces a new, structured information stream (number 6 in Windows PowerShell streams) that you can use to transmit structured data between a script and its callers (or hosting environment). Write-Information lets you add an informational message to the stream, and specify how Windows PowerShell handles information stream data for a command.

You can still use colors, without using Write host, by doing this ...

PowerTip: Write PowerShell Output in Color Without Using Write-Host

https://blogs.technet.microsoft.com/heyscriptingguy/2012/10/11/powertip-write-powershell-output-in-color-without-using-write-host

Summary: Write colorized output to the Windows PowerShell console without using the Write-Host cmdlet.

Hey, Scripting Guy! Question How can you write output to the Windows PowerShell console without using the Write-Host cmdlet?

Hey, Scripting Guy! Answer Set ForegroundColor to a different color by using $host.Ui.RawUi, and then use the Write-Output cmdlet to write the output. When you are finished, set the ForegroundColor back to its original color.

$t = $host.ui.RawUI.ForegroundColor

$host.ui.RawUI.ForegroundColor = "DarkGreen"

Write-Output "this is green output"

this is green output

$host.ui.RawUI.ForegroundColor = $t

...but it's a bit more cumbersome IMHO.

I wrote a function called Set-TextColor that I keep in my profile for easy access and use.

like image 80
postanote Avatar answered Mar 25 '26 00:03

postanote