I have a cmdlet that uses Format-Table
to output potentially long strings (such as registry paths). I would like to set each column width to the output buffer width divided by the number of columns.
Example:
function Write-Something {
[CmdletBinding()] param()
$o = [pscustomobject]@{ a = 'A' * 100; b = 'B' * 100 }
$columnWidth = [int]( $PSCmdlet.Host.UI.RawUI.BufferSize.Width / 2 )
$o | Format-Table @{ e = 'a'; width = $columnWidth }, @{ e = 'b'; width = $columnWidth } -wrap
}
This works nicely for console output, where it produces output like this:
a b - - AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB AAAAAAAAAAAA BBBBBBBBBBBB
Problem
When I specify a different output buffer width, using the Width
argument of Out-String
or Out-File
, the formatting won't change, it will still be based on the console buffer width.
Write-Something | Out-File test.txt -Width 200
This produces the same output as above whereas the expected output should be columns of width 100, with no wrapping occuring.
How can I get the actual output buffer width set by the Width
argument of Out-String
or Out-File
from within my cmdlet?