Search code examples
powershellword-wrap

Stop word wrapping of Write-Warning and Write-Debug on powershell.exe


Background

  • On powershell.exe, when you write messages with Write-Warning or Write-Debug, Japanese strings without spaces are wrapped automatically.
    • On PowerShell ISE, these cmdlets don't wrap messages.
  • Write-Error wraps messages in both environments.
  • Write-Host, Write-Output, and Write-Information don't wrap messages in both environments.

Example

$message = "The quick brown fox jumps over the lazy dog. いろはにほへとちりぬるをわかよたれそつねならむうゐのおくやまけふこえてあさきゆめみしゑひもせす."
Write-Host ("Write-Host Message: {0}" -F $message)
Write-Output ("Write-Output Message: {0}" -F $message)
Write-Error ("Write-Error Message: {0}" -F $message) -ErrorAction Continue
Write-Information ("Write-Information Message: {0}" -F $message) -InformationAction Continue
Write-Warning ("Write-Warning Message: {0}" -F $message) -WarningAction Continue
$DebugPreference = 'Continue'
Write-Debug ("Write-Debug Message: {0}" -F $message)

On powershell.exe: Result on powershell.exe

On PowerShell ISE:

Result on PowerShell ISE

Question

I want to show Japanese messages of Write-Error, Write-Warning, or Write-Debug without word wrapping on powershell.exe.

Is there any way to stop word wrapping of these cmdlets like overflow-wrap: break-word; in CSS?


Solution

  • You can do that by telling Powershell the size of the window, rather than letting it detect it. Simply add these three lines before the rest of your code, and you'll find when your existing lines of code are run, all the text appears on a single line.

    $max = $host.UI.RawUI.MaxPhysicalWindowSize
    $host.UI.RawUI.BufferSize = New-Object System.Management.Automation.Host.Size(9999,9999)
    $host.UI.RawUI.WindowSize = New-Object System.Management.Automation.Host.Size($max.Width,$max.Height)
    
    $message = "The quick brown fox jumps over the lazy dog. いろはにほへとちりぬるをわかよたれそつねならむうゐのおくやまけふこえてあさきゆめみしゑひもせす."
    Write-Host ("Write-Host Message: {0}" -F $message)
    Write-Output ("Write-Output Message: {0}" -F $message)
    Write-Error ("Write-Error Message: {0}" -F $message) -ErrorAction Continue
    Write-Information ("Write-Information Message: {0}" -F $message) -InformationAction Continue
    Write-Warning ("Write-Warning Message: {0}" -F $message) -WarningAction Continue
    $DebugPreference = 'Continue'
    Write-Debug ("Write-Debug Message: {0}" -F $message)
    

    Note, the one weird effect I've seen with this in testing is that from the ISE it just runs normally, but in powershell.exe if you have the window smaller than the screen size, it will increase the size to fit your screen. Shrinking the window down again will re-introduce wrapping. But, if you're running it on a small screen (so the powershell.exe is already full screen and wrapping) it will do what you're looking for and put all the text on one line with a scroll bar at the bottom.