Setting PowerShell window width

Start-Transcript is a PowerShell commandlet which causes PowerShell to log all output to a file. It uses the current window size (i.e. column count) to calculate line breaks in the generated file.

The PowerShell window is 120 characters wide by default. Thus a log file generated by Start-Transcript will reflect this 120 character limit.

You can even change the PowerShell window size using the Get-Host commandlet:

$h = get-host
$win = $h.ui.rawui.windowsize
$win.width  = 120  # change to preferred width
$h.ui.rawui.set_windowsize($win)

However, if PowerShell is invoked from a batch file being executed in cmd, the PowerShell window is only 80 characters wide, since the cmd window is (typically) 80 characters. Calling set_WindowSize will have no effect (apart from an error message), because the PowerShell window cannot have a greater size than the parent window.

To adjust the cmd window width before executing a PowerShell script, use the good old MODE command from DOS era:

mode con cols=120
powershell path-to\myscript.ps1

PowerShell will now inherit the desired window width, and line breaks in the log occur where you expect them from working in the PowerShell window.

1 thought on “Setting PowerShell window width

  1. You can also create a shortcut to the PS1 (powershell.exe script.ps1) and modify the properties of the shortcut layout tab to have a wider screen buffer size and window size.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.