How to change cursor position in powershell console

喜你入骨 提交于 2020-01-06 05:57:27

问题


I currently assign the current CursorPosition to a variable in Powershell so that I can overwrite the same space when performing a count down for example in a script, as below:

$errorPos = $host.UI.RawUI.CursorPosition
for ($i=5; $i -ge 0; $i--) {
    $host.UI.RawUI.CursorPosition = $errorPos
    Write-Host -NoNewline -BackgroundColor Yellow -ForegroundColor Black "$i"
    Start-Sleep -Seconds 1
}

What I'd like to do, it take the current position of the cursor and move it forward two spaces, then assign it to another variable. I could just use:

write-host "  "

but I don't want to overwrite the text currently occupying that space.

I think it can be accomplished using X and Y co ords but am not having much success.

Thanks for any assistance..!


回答1:


If you just want to move the 'X' forward by 2 you can just do this after creating your errorPos variable:

$errorPos.X += 2

You can modify the variable directly by using $errorPos.X and .Y.



来源:https://stackoverflow.com/questions/48302647/how-to-change-cursor-position-in-powershell-console

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!