Controlling window position of a Powershell console

12,957

Solution 1

The error is not really strange, because WindowPosition Gets and sets the position, in characters, of the view window relative to the screen buffer.

It does not set the position of the Window, but to put it crudely, the position in the buffer that you see through the view of the window. So in your case, you are getting the error because it is outside the buffer.

http://msdn.microsoft.com/en-us/library/system.management.automation.host.pshostrawuserinterface.windowposition%28v=vs.85%29.aspx

Unfortunately, setting the position of the window is not simple. There is a snapin for it though - http://wasp.codeplex.com/ ( use Set-WindowPosition)

Solution 2

Take a look at this script: Resize-Console.ps1 – Resize console window/buffer using arrow keys.

It is hopefully useful itself and partially should answer the question (the size part).

Share:
12,957
djangofan
Author by

djangofan

I always pay it forward. I ask questions so I can learn and I try to help others.

Updated on June 17, 2022

Comments

  • djangofan
    djangofan almost 2 years

    This works:

    (Get-Host).UI.RawUI
    $a = (Get-Host).UI.RawUI
    $a.BackgroundColor = "white"
    $a.ForegroundColor = "black"
    
    $size = (Get-Host).UI.RawUI.WindowSize
    $size.Width = 80
    $size.Height = 30
    (Get-Host).UI.RawUI.WindowSize = $size
    

    But this doesn't work, and I am not sure how to make it work:

    $position = (Get-Host).UI.RawUI.Windowposition
    $position.X = 0
    $position.Y = 30
    (Get-Host).UI.RawUI.Windowposition = $position
    

    The error I get is strange. It complains about "buffer" when I am trying to set external window position:

    Exception setting "WindowPosition": "Cannot use the 
    specified Window X (column) position because it extends 
    past the width of the screen buffer. Specify another X 
    position, starting with 0 as the left most column of 
    the buffer.
    
  • djangofan
    djangofan over 12 years
    yes, it is indeed doing that. ok, thats wierd... who would need that?
  • djangofan
    djangofan over 12 years
    That is totally awesome. Thats the way to do it. Thanks.