PowerShell Out-file -width does not truncate content when exporting to a file -
i have windows 7 , powershell 4.0. when exporting content file -width parameter not format based on given setting. here sample of trying do:
"it nice hot sunny day" | out-file -filepath ".\output.txt" -encoding ascii -width 10 the result of export not truncated @ 10th character. not truncated @ all. cannot figure out what's wrong.
this came of surprise me, apparently, -width parameter works formatted objects:
string(s) input, no effect
ps c:\> "it nice hot sunny day" |out-file '.\output.txt' -width 10 -force; gc '.\output.txt' nice hot sunny day format-table, works
ps c:\> new-object psobject -property @{text="it nice hot sunny day"} | format-table |out-file '.\output.txt' -width 10 -force; gc '.\output.txt' text ----- a... format-list, works, in strange manner:
ps c:\> new-object psobject -property @{text="it nice hot sunny day"} | format-table |out-file '.\output.txt' -width 10 -force; gc '.\output.txt' text : n ice ho t s unn y day so, closest can propably format-table -hidetableheaders:
ps d:\> new-object psobject -property @{text="it nice hot sunny day"} | format-table -hidetableheaders|out-file '.\output.txt' -width 10 -force; gc '.\output.txt' a... inspired @matt's answer, write own function 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 nice hot sunny day" |resize-string|out-file '.\output.txt' -width 10 -force; gc '.\output.txt' ni
Comments
Post a Comment