Behind the PowerShell Pipeline logo

Behind the PowerShell Pipeline

Subscribe
Archives
July 23, 2024

More ANSI Alternatives

I've been demonstrating how to use ANSI to display messages in the console while your PowerShell code is executing. What I've shown you should be considered a proof of concept more than anything. Expect a lot of trial and error to get the desired output. My ideas will work best if your command doesn't write anything to the pipeline, or if it sends output after all of your ANSI-based messaging is complete. But that probably is wishful thinking on my part. Before we get to today's content, I want to point out a subtle distinction in clearing the console. In the previous article, I mentioned you could use this ANSI sequence to clear the display.

"e[1J"
You may wonder what the difference is between this and using `Clear-Host`. It depends on where the cursor is when you clear the screen. If you are at the bottom of the console, using `Clear-Host` will scroll the console up, leaving the cursor at the top. Using the ANSI sequence will clear the screen and leave the cursor at the bottom. You can try these two commands to see the difference.
Get-Process ; Start-Sleep -milliseconds 500; Clear-Host; Write-Host "I am here"

Get-Process ; Start-Sleep -milliseconds 500; "`e[1J"; Write-Host "I am here"
Notice where the message is displayed? This is a minor point, but it might be important in your script. This should make it clear you need to test extensively when using ANSI sequences in your code. You might also want to add restrictions or tests to ensure your code is running in a supported environment. I also showed you in the last article a function that displayed status messages using ANSI escape sequences at the bottom of the screen. I was using `Write-Host` to display a string via a private function.
Function Status {

    Param([string]$Message)

    #$Row is found in the parent scope

    Write-Host "`e[$Row;1H`e[2K`e[93;1;3mSTATUS: $Message`e[0m"

    Start-Sleep -Milliseconds 250

}
> *Private functions do not need to follow the Verb-Noun naming convention.* The downside to this approach is that I am not using the `Verbose` stream. This means I don't get the `VERBOSE` indicator. And if I am redirecting streams, the Status function can't be redirected as `Verbose` output. However, with a little tweaking, I can still use the `Verbose` stream. I will define a proxy function in my script for `Write-Verbose`.
Get a premium subscription for full article and archive access

I've been demonstrating how to use ANSI to display messages in the console while your PowerShell code is executing. What I've shown you should be considered a proof of concept more than anything. Expect a lot of trial and error to get the desired output. My ideas will work best if your command doesn't write anything to the pipeline, or if it sends output after all of your ANSI-based messaging is complete. But that probably is wishful thinking on my part.

Before we get to today's content, I want to point out a subtle distinction in clearing the console. In the previous article, I mentioned you could use this ANSI sequence to clear the display.

"e[1J"
Want to read the full issue?
GitHub Bluesky LinkedIn About Jeff
Powered by Buttondown, the easiest way to start and grow your newsletter.