How to make the width of powershell script's output greater than 80 column?

Hi,

 

I am trying to remotely execute a powershell script through ssh, and I found the output of my script is only 80 width, a carriage-return and a line-feed are inserted after 79th column, and continue presenting the 80th value of each row onto another line. I want it greater than 80, such as 512.

The code to print the output in my script is like:

$allInfo | ConvertTo-Xml -as Stream -NoTypeInformation -Depth 1

 

Any help will be really appreciated!

 

 

May 21st, 2011 9:20am

I'm not sure how to control the width of the output when it's going to a console though I'm guessing it has something to do with the console width itself. I believe I've read how to directly manipulate the Powershell console options somewhere.

However, if you redirect output to a file with the out-file command, it has a -width option that lets you explicitly specify how wide the file should be.  Perhaps that's all you need.

$allInfo | ConvertTo-Xml -as Stream -NoTypeInformation -Depth 1 | out-file -width 512 

Free Windows Admin Tool Kit Click here and download it now
May 21st, 2011 8:02pm

Thanks for you reply!

 

But I do not want to output to a file. What I am doing is, in a C program, I execute a powershell script remotely through SSH, and then get all its output back.

So I guess maybe I can use "out-string -width 512", then I tried, but no luck.

 

I am very confused with this behavior because if I put all the output of that powershell script in a .txt file, and in my C program, execute the command "type ***.txt", all the output can be gotten correctly (no additional carriage-return and a line-feed are inserted), so that means the normal Windows command is fine, but what is the problem with Powershell script? Making one line into two line is unacceptable, why does PowerSheel do like that?

May 22nd, 2011 1:33am

The window size is 80 by default. What happens if you increase the window size value? 
Free Windows Admin Tool Kit Click here and download it now
May 22nd, 2011 5:17pm

I believe this article explains how to do exactly what you want.  Let us know if it works.
  • Marked as answer by eric725 Monday, May 23, 2011 1:57 AM
May 22nd, 2011 10:33pm

Thanks! That article really help me out, after putting the following line into my powershell script, no addtional carriage-return and a line-feed are inserted anymore.

$Host.UI.RawUI.BufferSize = New-Object Management.Automation.Host.Size (500, 25)

 

But when I run the powershell script manually, I get the following error message, I do not know why, but anyway, my problem is resolved.

Exception setting "BufferSize": "Cannot set the buffer size because the size specified is too large or too small.           
Parameter name: value                                                                                                       
Actual value was 512,25."                                                                                                   
At C:\Users\qzhang\Documents\GetVM.ps1:30 char:16                                                                           
+ $Host.UI.RawUI. <<<< BufferSize = New-Object Management.Automation.Host.Size (512, 25)                                    
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException                                                    
    + FullyQualifiedErrorId : PropertyAssignmentException  

Free Windows Admin Tool Kit Click here and download it now
May 23rd, 2011 2:00am

But when I run the powershell script manually, I get the following error message, I do not know why, but anyway, my problem is resolved.

Exception setting "BufferSize": "Cannot set the buffer size because the size specified is too large or too small.           
Parameter name: value                                                                                                       
Actual value was 512,25."                                                                                                   
At C:\Users\qzhang\Documents\GetVM.ps1:30 char:16                                                                           
+ $Host.UI.RawUI. <<<< BufferSize = New-Object Management.Automation.Host.Size (512, 25)                                    
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException                                                    
    + FullyQualifiedErrorId : PropertyAssignmentException  

Yeah, that's a good question.  I get the same thing here.  The longer solution the post covers which I've included below does work interactively, and looking at the differences it appears to be related to the buffer height.  My guess is when the existing console window already has more rows in the buffer than the 25 rows in height the command tries to define, this error occurs.

if( $Host -and $Host.UI -and $Host.UI.RawUI ) {
 $rawUI = $Host.UI.RawUI
 $oldSize = $rawUI.BufferSize
 $typeName = $oldSize.GetType( ).FullName
 $newSize = New-Object $typeName (500, $oldSize.Height)
 $rawUI.BufferSize = $newSize
}
May 23rd, 2011 12:09pm

Thanks! That longer solution does work, the error never happens after I include it in my powershell script.

BTW, you said that it does work "interactively", so what about NonInteractive powershell script? Will there be any problems? My powershell script is NonInteractive, and everything looks fine so far.

 

Free Windows Admin Tool Kit Click here and download it now
May 24th, 2011 1:06am

Thanks! That longer solution does work, the error never happens after I include it in my powershell script.

BTW, you said that it does work "interactively", so what about NonInteractive powershell script? Will there be any problems? My powershell script is NonInteractive, and everything looks fine so far.

 

I don't think so, no.  I think the only reason the one-liner fails when you run it interactively is that you're doing so with a buffer that already exceeds the 25 row height.  My guess is if you cleared the buffer first it would work fine.  The longer solution works when you run it interactively simply because it preserves the existing buffers height.

Either solution should work fine for you.  The second one is coded to work in more scenarios, such as from an interactive shell that has a larger buffer and has some code that I believe is there to improve it's chances of working within other PowerShell consoles.  I'm not sure exactly why it goes to all the trouble it does to determine the type of the new buffersize object, but perhaps the author works with a 3rd party console that they know to use a different type.  In any event, it sounds like you're good to go either way.

May 24th, 2011 2:52pm

Another possible reason for the error: BufferSize cannot be made smaller than WindowSize.
Free Windows Admin Tool Kit Click here and download it now
April 23rd, 2015 1:38pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics