PowerShell Out-file -width doesn't truncate content when exporting to file
I have Windows 7 and PowerShell 4.0. When you export content to a file, the parameter is -Width
not formatted based on this setting. Here's an example of what I'm trying to do:
"It is a nice hot sunny day" | Out-File -FilePath ".\output.txt" -Encoding ASCII -Width 10
The export result is not truncated at the 10th character. It is not cropped at all. I can't figure out what happened.
source to share
This came as a surprise to me, but apparently the parameter -Width
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
, it 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 way:
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 possibly 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 @ Matatt's answer, you can 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
source to share
I'm puzzled about the parameter too -Width
, as I would expect you to type in to do something. However, if it worked, I don't think you will get the expected results 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 an integer based string input and wrap.
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
I would make this conclusion, which has the advantage of not breaking words.
It is
a nice
hot sunny
day
This can now be easily transferred to a file. More explanation follows from my answer to another question
source to share
Unfortunately the documentation is not very clear, but it seems to -Width
only apply when an object of some type is mapped to text output and no lines are counted.
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 doesn't use it -Width
at all.
source to share