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

6.5k views Asked by At

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:

"It is a nice hot sunny day" | Out-File -FilePath ".\output.txt" -Encoding ASCII -Width 10

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.

3

There are 3 answers

0
Mathias R. Jessen On

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

PS C:\> "It is a nice hot sunny day" |Out-File '.\output.txt' -Width 10 -Force; gc '.\output.txt'
It is a nice hot sunny day

Format-Table, this works

PS C:\> New-Object psobject -Property @{text="It is a nice hot sunny day"} | Format-Table |Out-File '.\output.txt' -Width 10 -Force; gc '.\output.txt'

text
-----
It is a...

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

PS C:\> New-Object psobject -Property @{text="It is a nice hot sunny day"} | Format-Table |Out-File '.\output.txt' -Width 10 -Force; gc '.\output.txt'

text : It
       is
       a n
       ice
        ho
       t s
       unn
       y
       day

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

PS D:\> New-Object psobject -Property @{text="It is a nice hot sunny day"} | Format-Table -HideTableHeaders|Out-File '.\output.txt' -Width 10 -Force; gc '.\output.txt'

It is a...

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

Function Resize-String {
    Param(
        [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
        [string[]]$InputObject,
        [int]$Width = 10
    )

    process{
        $InputObject.Substring(0,[System.Math]::Min([string]$InputObject[0].Length,$Width))
    }
}

PS C:\> "It is a nice hot sunny day" |Resize-String|Out-File '.\output.txt' -Width 10 -Force; gc '.\output.txt'
It is a ni
0
briantist On

Unfortunately the documentation is not very clear on this, but it appears that -Width only applies when an object of some kind is being rendered to text output, and strings don't count.

For example:

([PSCustomObject]@{Value = "It is a nice hot sunny day"}) | Out-File -FilePath ".\output.txt" -Encoding ASCII -Width 10

For strings, and even arrays, it's not using -Width at all.

0
Matt On

I too am puzzled by the -Width parameter as I would have expected what you typed to do something. However if it did work I don't think you would get the results you expect anyway. If you read Out-File on TechNet you will see that the width...

Specifies the number of characters in each line of output. Any additional characters are truncated

So I have a similar answer where I take string input and wrap based on an integer.

Function Get-Folded{
    Param(
        [string[]]$Strings,
        [int]$Wrap = 50
    )
    $strings  -join " " -split "(.{$wrap,}?[ |$])" | Where-Object{$_}
}

So if we try your text

Get-Folded "It is a nice hot sunny day" -Wrap 5

Would render this output which has the advantage of not breaking up the words.

It is 
a nice 
hot sunny 
day

This could easily be piped to file now. More explanation comes from my answer to another question