How to auto adjust the powershell output width

I am using v2019.3.1 LTS. I had some process that use powershell to do the works, and sometimes it will generate error, and that error message was wrapped due to powershell width.

I ran $host.UI.RawUI.BufferSize and it is showing the default width is 80. When I add this line before outputing the error message($Host.UI.RawUI.BufferSize = New-Object Management.Automation.Host.Size (500, 25)) , I get the desired output message in one line.

I reading from this post(Powershell script output line wrap) that the buffer size should be already in 500. How can I change the default width so that I don’t have to add the ($Host.UI.RawUI.BufferSize = New-Object Management.Automation.Host.Size (500, 25)) to every powertshell step?

Thanks in advance

Hi @pchan,

Thanks for reaching out. A long time ago (~2012) we used to spin up our own powershell host each time we ran scripts. That proved to be very painful to maintain and not too beneficial, so we switched to just running our scripts using the PS host of the VM instead. This means that the PS session we run will use the same $host config that comes with your VM/user session.

One thing you could do is take advantage of this “gotcha” using a script module and set the value of $Host.UI.RawUI.BufferSize from the module.

Best regards,

Hi @Dalmiro
Thanks for you comment I will try it out.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.