3

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 coordinate but am not having much success.

Rob Berry
  • 185
  • 1
  • 4
  • 14

1 Answers1

3

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.

colsw
  • 3,216
  • 1
  • 14
  • 28
  • Great, thats solved it, thanks ConnorLSW. I think I may have had something like this when I was playing around but the ISE doesn't like manipulating the position of the cursor so it seems! – Rob Berry Jan 17 '18 at 14:56
  • 1
    definitely test this in the normal prompt, the ISE will be very unlikely to play nice :) - you may want to take a look at `Write-Progress` too which is the 'better' way to do what you're looking to do. – colsw Jan 17 '18 at 15:44