PowerShell Inline Status Updates Without Write-Progress

less than 1 minute read

Preface: Don’t use this, it’s really bad practice for PowerShell code to work by manipulating the host console window. It’ll work, but it’ll mess with logging, transcripts, text pipelining (ew, gross), and other things I’m sure. For a one off that you’re writing in a busy remote shell… maybe...

Here’s a lazy way of having a progress indicator that doesn’t fill the whole screen history and doesn’t require Write-Progress. Use NoNewline to avoid implicit newline/carriage return, then manually add a carriage return (`r) at the start of your line to overwrite existing text.

The magic "$([char]27[2K`r)" is the ANSI control sequence Escape[2K and tells the terminal to preemptively erase the entire line so that you don’t end up with leftover characters if your next value is shorter than the previous one. https://www.real-world-systems.com/docs/ANSIcode.html#CSI

Write-Host "`r$TimestampOrWhatever: $ValueOrWhatever" -NoNewline


Write-Host "$([char]27)[2K`r" -NoNewline #Equivalent to Esc\[2K
Write-Host "$TimestampOrWhatever: $ValueOrWhatever" -NoNewline