You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Out-String -Width 10 does not truncate a string #6971
Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-AnsweredThe question is answered.WG-Enginecore PowerShell engine, interpreter, and runtime
I apologize for this if it is expected behavior however based on the documentation, I believe that setting width should cause the output to be truncated.
-Width
Specifies the number of characters in each line of output. Any additional characters are truncated, not wrapped. If you omit this parameter, the width is determined by the characteristics of the host program. The default value for the Windows PowerShell console is 80 (characters).
Hi Jason Chester (@jasonchester) When you use the -Width parameter on Out-String you're overriding the screen width used when formatting objects. For example, dir *.md | Out-String -Width 20 will give you output that looks like:
PS > ls *.md | out-string -width 20
Directory: D:\gi
t\PowerShell_bru
cepay
Mode LastWriteTim
e
---- ------------
-a---- 5/20/18
11:30 AM
-a---- 5/20/18
11:30 AM
-a---- 5/20/18
11:30 AM
But that only applies to objects that are being formatted. Strings are treated as "already formatted" so they just pass through to the output unchanged. This is why your example, which uses a string, doesn't appear to work.
Oh - and if you want to create a long string, multiplication works on strings:
Bruce Payette (@BrucePay), Thanks for the quick answer, the explanation about out string applying to un-formatted inputs but not strings was exactly what I was missing.
More importantly, thanks for the string multiplication tip. I’ll use that for sure.
Do you have any posh-fu for truncating strings to a specified length? I am planning to use Write-ProgressEx to display the progress of a script executing batches of gremlin queries. I am setting the status of the loop to the gremlin query but it is longer than my display is wide so it ends up wrapping which makes it much harder to scan as it scrolls by.
Of course there is "...".Substring(0,$width) but, it just doesn't feel very posh in my pipeline which is gets even more cluttered when combined with the (get-host).stuff.ui.junk.width needed to handle console resizing gracefully
Jason Chester (@jasonchester) Yes - this proposal #6753Add string cmdlets that emulate operator functionality on pipeline should be what you want. If you can, please take a few minutes to read the proposal and provide feedback on scenarios, etc. Thanks!
For now, to truncate strings in the pipeline, you could use foreach with the substring() method as follows:
Unfortunately Substring() throws if the string isn't long enough. An alternate approach would be a function like this:
functionTruncateStr ($len) { process { -join (([string]$_)[0..$len]) }}
This little function will take strings from the pipeline and truncate them to the desired length. If the string isn't long enough, there's no error - the string is just passed through.
Out of curiosity I compared the performance of various approaches and made the following surprising discoveries:
Even letting .Substring() fail and handling the exception with try / catch makes for faster execution than placing conditionals around it (and the latter are in turn faster than the -join (([string]$_)[0..($len-1)]) approach).
Unexpectedly, in Windows PowerShell and PS Core v6.0.2 the following arcane regex solution is fastest: $_ -replace "(?<=.{$len}).+"
$len=3# how many chars. to return$count=1000# how many runs to average'foooooooooooooooo'| Time-Command -Count $count`
{ $_.Substring(0, [Math]::Min($len,$_.Length)) },
{ try { $_.Substring(0,$len) } catch { $_ } },
{ if ($_.Length-lt$len) { $_ } else { $_.Substring(0,$len) }},
{ -join (([string]$_)[0..($len-1)]) },
{ $_-replace"(?<=.{$len}).+" }
Here are example results from a W10 VM (note that Core is slower in general, and note how the regex solution went from fastest to slowest between v6.0.2 and v6.1.0-preview.2):
Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-AnsweredThe question is answered.WG-Enginecore PowerShell engine, interpreter, and runtime
Activity
BrucePay commentedon Jun 2, 2018
Hi Jason Chester (@jasonchester) When you use the
-Widthparameter onOut-Stringyou're overriding the screen width used when formatting objects. For example,dir *.md | Out-String -Width 20will give you output that looks like:But that only applies to objects that are being formatted. Strings are treated as "already formatted" so they just pass through to the output unchanged. This is why your example, which uses a string, doesn't appear to work.
Oh - and if you want to create a long string, multiplication works on strings:
SteveL-MSFT commentedon Jun 2, 2018
This is not clear from the current documentation. We should update the documentation to make this distinction more clear.
jasonchester commentedon Jun 2, 2018
Bruce Payette (@BrucePay), Thanks for the quick answer, the explanation about out string applying to un-formatted inputs but not strings was exactly what I was missing.
More importantly, thanks for the string multiplication tip. I’ll use that for sure.
Do you have any posh-fu for truncating strings to a specified length? I am planning to use Write-ProgressEx to display the progress of a script executing batches of gremlin queries. I am setting the status of the loop to the gremlin query but it is longer than my display is wide so it ends up wrapping which makes it much harder to scan as it scrolls by.
Of course there is "...".Substring(0,$width) but, it just doesn't feel very posh in my pipeline which is gets even more cluttered when combined with the (get-host).stuff.ui.junk.width needed to handle console resizing gracefully
SteveL-MSFT commentedon Jun 2, 2018
Jason Chester (@jasonchester) you need this: #6753
BrucePay commentedon Jun 2, 2018
Jason Chester (@jasonchester) Yes - this proposal #6753 Add string cmdlets that emulate operator functionality on pipeline should be what you want. If you can, please take a few minutes to read the proposal and provide feedback on scenarios, etc. Thanks!
For now, to truncate strings in the pipeline, you could use
foreachwith thesubstring()method as follows:Unfortunately
Substring()throws if the string isn't long enough. An alternate approach would be a function like this:This little function will take strings from the pipeline and truncate them to the desired length. If the string isn't long enough, there's no error - the string is just passed through.
mklement0 commentedon Jun 2, 2018
Bruce Payette (@BrucePay): Small correction:
[0..$len]->[0..($len-1)].Tangent alert:
Out of curiosity I compared the performance of various approaches and made the following surprising discoveries:
Even letting
.Substring()fail and handling the exception withtry/catchmakes for faster execution than placing conditionals around it (and the latter are in turn faster than the-join (([string]$_)[0..($len-1)])approach).Unexpectedly, in Windows PowerShell and PS Core v6.0.2 the following arcane regex solution is fastest:
$_ -replace "(?<=.{$len}).+"In PS Core v6.1.0-preview2 - the current preview - the regex solution is more than twice as slow as before (and then slowest overall) - any ideas why? A change in PowerShell or in CoreFx? See Combining the -replace operator with a lookbehind assertion slowed down by a factor of 30+ between v6.0.2 and v6.1.0-preview.2 #6976.
Test code (uses script
Time-Command):Here are example results from a W10 VM (note that Core is slower in general, and note how the regex solution went from fastest to slowest between v6.0.2 and v6.1.0-preview.2):
sdwheeler commentedon Aug 2, 2018
Docs fixed in PR MicrosoftDocs/PowerShell-Docs#2759