If you've been following my work for a while, you know I am a big fan of using `Write-Verbose`. I find the verbose output very helpful when I am writing a PowerShell function. The verbose output writes to a separate stream and is identifiable in the console.
VERBOSE: [14:17:41.9281665 BEGIN ] Running under PowerShell version 7.4.3
Lately, I've been experimenting with alternatives. Instead of verbose output that is intermingled with the output and that scrolls with the output, I'd like to have a message that is displayed in a specific area, with each message overwriting the previous one. I don't always care about the verbose output after the command finishes. Maybe I only need to see the messages while the code is executing.
One way I can accomplish this is by leveraging ANSI escape sequences. You are probably familiar with them from `$PSStyle` to add foreground and background color or styles. But there are also escape sequences that allow you to move the cursor around the console window. You can find this documented on [Wikipedia](https://en.wikipedia.org/wiki/ANSI_escape_code).
- `ESC[nA` Moves the cursor up by n lines
- `ESC[nB` Moves the cursor down by n lines
- `ESC[nC` Moves the cursor right by n characters
- `ESC[nD` Moves the cursor left by n characters
- `ESC[2K` Clears the line
- `ESC[2J` Clears the screen
The `ESC` character in PowerShell 7 is `` `e`` or you can use `[char]27`.
"$([char]27)[2J"
> *The letters in the escape sequence like `A` or `J` are case-sensitive.*
For my demonstrations, I am using PowerShell 7 running in Windows Terminal. I expect most of you are using Windows Terminal. This app automatically enables ANSI support. If you are using a different console and not getting the results you expect, you might need to configure output encoding.
You'll see the message 'Write message 1` to `Write message 10` displayed on the same line, overwriting the previous message. When the loop finishes, the line is cleared.
You need to be careful about using `NoNewLine` or even if you are using `Write-Host`. Consider this list of names.
$Names="Alice","Bob","Charlie","David","Eve"
I might try code like this to stream the names to the console, sharing the same line.
If you try this, and I hope you do, you'll see the name written in front of your prompt. That won't work.
You might try this which first writes an empty line.
Now all of the names are written on the same line, overwriting the previous name. However, if the previous name was longer than the current name, you'll see leftover characters. Let's fix this with ANSI escape sequences.
figure 1
Figuring out a prototype gave me experience and a better understanding of how to use ANSI escape sequences. Let's apply these principles to some PowerShell code.
If you've been following my work for a while, you know I am a big fan of using Write-Verbose. I find the verbose output very helpful when I am writing a PowerShell function. The verbose output writes to a separate stream and is identifiable in the console.
VERBOSE: [14:17:41.9281665 BEGIN ] Running under PowerShell version 7.4.3
Lately, I've been experimenting with alternatives. Instead of verbose output that is intermingled with the output and that scrolls with the output, I'd like to have a message that is displayed in a specific area, with each message overwriting the previous one. I don't always care about the verbose output after the command finishes. Maybe I only need to see the messages while the code is executing.