Format-Table set column width depending on the output buffer width

Question:

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:

This works nicely for console output, where it produces output like this:

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.

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?

Answer:

The problem is that you’ve already fixed the width in your Write-Something cmdlet. The PowerShell way to do this would be for your cmdlet to output your unformatted data objects and for you to replace Out-File with your own cmdlet which controls the output width.

Source:

Format-Table set column width depending on the output buffer width by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply