[System.Console]::SetWindowPosition() fails to set cursor position #12379

Closed
opened 2026-01-31 03:14:01 +00:00 by claunia · 5 comments
Owner

Originally created by @nebula-it on GitHub (Feb 3, 2021).

Environment

Windows build number: 10.0.19042.0
Windows Terminal version (if applicable): 1.5.10271.0

Any other software? N/A

Steps to reproduce

Run [System.Console]::SetWindowPosition(0,[System.Console]::CursorTop) in terminal. Compare this to its run in regular powershell console.

Expected behavior

Running [System.Console]::SetWindowPosition(0,[System.Console]::CursorTop) should scroll the buffer to top. Detailed info available here as well: https://tommymaynard.com/clear-host-without-clearing-the-host-2014/

Actual behavior

.NET console class method "SetWindowPosition" fails to execute. In regular Powershell console, following command was used to scroll to top of the window (As an alternative to cls/Clear-Host, so buffer was not cleared only scrolled up). Confirmed it is still working there but in Terminal it fails with following error:

MethodInvocationException: Exception calling "SetWindowPosition" with "2" argument(s): "The window position must be set such that the current window size fits within the console's buffer, and the numbers must not be negative. (Parameter 'top')
Actual value was 2."

Using any other value than (0,0) causes this to fail. [System.Console]::SetWindowPosition(0,0) does succeed but does not scroll to top of the window as it does in regular powershell console.

P.S: This might not be a Terminal issue but would love to get it resolved.
and Thank you team for all the love you pour into Terminal, its easily my most used and most loved software on Windows along with PowerShell itself. Cheers

Originally created by @nebula-it on GitHub (Feb 3, 2021). <!-- 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 I ACKNOWLEDGE THE FOLLOWING BEFORE PROCEEDING: 1. If I delete this entire template and go my own path, the core team may close my issue without further explanation or engagement. 2. If I list multiple bugs/concerns in this one issue, the core team may close my issue without further explanation or engagement. 3. If I write an issue that has many duplicates, the core team may close my issue without further explanation or engagement (and without necessarily spending time to find the exact duplicate ID number). 4. If I leave the title incomplete when filing the issue, the core team may close my issue without further explanation or engagement. 5. If I file something completely blank in the body, the core team may close my issue without further explanation or engagement. All good? Then proceed! --> <!-- This bug tracker is monitored by Windows Terminal development team and other technical folks. **Important: When reporting BSODs or security issues, DO NOT attach memory dumps, logs, or traces to Github issues**. Instead, send dumps/traces to secure@microsoft.com, referencing this GitHub issue. If this is an application crash, please also provide a Feedback Hub submission link so we can find your diagnostic data on the backend. Use the category "Apps > Windows Terminal (Preview)" and choose "Share My Feedback" after submission to get the link. Please use this form and describe your issue, concisely but precisely, with as much detail as possible. --> # Environment ```none Windows build number: 10.0.19042.0 Windows Terminal version (if applicable): 1.5.10271.0 Any other software? N/A ``` # Steps to reproduce <!-- A description of how to trigger this bug. --> Run `[System.Console]::SetWindowPosition(0,[System.Console]::CursorTop)` in terminal. Compare this to its run in regular powershell console. # Expected behavior <!-- A description of what you're expecting, possibly containing screenshots or reference material. --> Running `[System.Console]::SetWindowPosition(0,[System.Console]::CursorTop)` should scroll the buffer to top. Detailed info available here as well: https://tommymaynard.com/clear-host-without-clearing-the-host-2014/ # Actual behavior <!-- What's actually happening? --> .NET console class method "SetWindowPosition" fails to execute. In regular Powershell console, following command was used to scroll to top of the window (As an alternative to cls/Clear-Host, so buffer was not cleared only scrolled up). Confirmed it is still working there but in Terminal it fails with following error: ``` MethodInvocationException: Exception calling "SetWindowPosition" with "2" argument(s): "The window position must be set such that the current window size fits within the console's buffer, and the numbers must not be negative. (Parameter 'top') Actual value was 2." ``` Using any other value than (0,0) causes this to fail. `[System.Console]::SetWindowPosition(0,0)` does succeed but does not scroll to top of the window as it does in regular powershell console. P.S: This might not be a Terminal issue but would love to get it resolved. and Thank you team for all the love you pour into Terminal, its easily my most used and most loved software on Windows along with PowerShell itself. Cheers
Author
Owner

@237dmitry commented on GitHub (Feb 8, 2021):

"Ctrl+L"

@237dmitry commented on GitHub (Feb 8, 2021): "Ctrl+L"
Author
Owner

@237dmitry commented on GitHub (Feb 8, 2021):

You can use esc-sequences as well.

"`e[2J`e[H"
@237dmitry commented on GitHub (Feb 8, 2021): You can use esc-sequences as well. ``` "`e[2J`e[H" ```
Author
Owner

@nebula-it commented on GitHub (Feb 9, 2021):

This is perfect, thanks @237dmitry. Just out of curiosity, is this documented somewhere?

@nebula-it commented on GitHub (Feb 9, 2021): This is perfect, thanks @237dmitry. Just out of curiosity, is this documented somewhere?
Author
Owner

@237dmitry commented on GitHub (Feb 9, 2021):

is this documented somewhere?

You can see here. Not of all working in Windows Terminal but this is problem of all terminals

@237dmitry commented on GitHub (Feb 9, 2021): > is this documented somewhere? You can see [here](https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797). Not of all working in Windows Terminal but this is problem of all terminals
Author
Owner

@zadjii-msft commented on GitHub (Feb 23, 2021):

So this one is weird. You can't set the Terminal's buffer viewport with SetWindowPosition, because technically there's another buffer that's sitting between powershell and the Terminal. We call this buffer "conpty". The conpty buffer is exactly the size of the viewport, which is more like how other terminal applications (on other platforms behave). So when you try to set the viewport's top to 2, conpty rejects that API call, because the viewport can only even be at the top of the buffer. It's always at top=0.

Like @237dmitry mentioned, you can use \x1b[2J to clear the "scrollback", the text above the viewport, and \x1b[H to move the cursor to 0,0. These are escape sequences, and we've got a big ol' list of the ones we support here. There's also a lot more details on using escape sequences instead of API calls in this doc.

@zadjii-msft commented on GitHub (Feb 23, 2021): So this one is weird. You can't set the Terminal's buffer viewport with `SetWindowPosition`, because technically there's another buffer that's sitting between powershell and the Terminal. We call this buffer "conpty". The conpty buffer is exactly the size of the viewport, which is more like how other terminal applications (on other platforms behave). So when you try to set the viewport's top to `2`, conpty rejects that API call, because the viewport can only even be at the top of the buffer. It's always at top=0. Like @237dmitry mentioned, you can use `\x1b[2J` to clear the "scrollback", the text above the viewport, and `\x1b[H` to move the cursor to 0,0. These are escape sequences, and we've got a big ol' list of the ones we support [here](https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences). There's also a lot more details on using escape sequences instead of API calls [in this doc](https://docs.microsoft.com/en-us/windows/console/classic-vs-vt).
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#12379