PowerShell Out-file -width does not truncate content when exporting to a file

Question:

I have Windows 7 and PowerShell 4.0. When exporting content to a file the -Width parameter does not format based on the given setting. Here is a sample of what I am trying to do:

The result of the export is not truncated at 10th character. It does not get truncated at all. I cannot figure out what’s wrong.

Answer:

This came as somewhat of a surprise to me, but apparently, the -Width parameter only works with formatted objects:

String(s) as input, no effect

Format-Table, this works

Format-List, this works, but in a strange manner:

So, the closest we can get is propably with Format-Table -HideTableHeaders:


Inspired by @Matt’s answer, you could write your own function to truncate strings:

Source:

PowerShell Out-file -width does not truncate content when exporting to a file by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply