PowerShell Inline Status Updates Without Write-Progress

Preface: Don’t use this, it’s really bad practice for PowerShell code to work by manipulating the host output stream. It’ll work, but it’ll mess with logging, transcripts, text pipeline (ew, gross, use objects), 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” (Escape[2K) is an ANSI control sequence that 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

Leave a Reply

Your email address will not be published. Required fields are marked *