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

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.

One Response to Setting PowerShell window width

  1. Bill says:

    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 Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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

%d bloggers like this: