Category Archives: Scripting

More Flashing Fun

talkbubbleI received a lot of interest in my Invoke-Flasher script. One comment I received on Twitter was for a way to use it interactively in a script. In essence, he wanted a flashing Read-Host so I took my original concept and tweaked it until I came up with a Read-Host alternative I simply call Read-Host2. This function will only work in the PowerShell console, NOT the PowerShell ISE.

The main tweak I made was to collect all the typed keys until Enter is pressed. I have a Switch construct to also eliminate the Shift key. The assumption is that you are writing text so this should be the only non alphanumeric key you would use. The message prompt will keep flashing until you start typing. I also emulated echoing text to the screen, including password masking if you use AsSecureString. The last change is a new parameter to allow you to flash the foreground color instead of the background color. There are several examples in the comment-based help.

Here are some screen shots.


I hope you’ll let me know what you think.

Look at Me!

bluelight Last week I posted some ideas on how to add notifications to your scripts. Those ideas were variations on the old school “Press any key to continue” prompt that I assume many of you are familiar with. Most of those concepts should work for you, but they assume you looking at the PowerShell window. I thought about those situations where perhaps I only see a portion of the PowerShell window. Wouldn’t it be helpful if you had some other visual clue, like a flashing light? I thought so and whipped up Invoke-Flasher. I’ll admit the name might have an unexpected connotation, but you can always change it.

Here’s the function, and then I’ll explain how to use it.

This function will only work in the PowerShell console, not the PowerShell ISE because it uses the ReadKey() method from $host.ui.rawui to detect if the user hits any key. The main portion of the function keeps looping through until a key is pressed. Each time through the background color of the host UI is toggled between the current color and Red, or whatever console color you specify. Each time through the script writes your text and “Press any key to continue”. I use the Coordinates property of the host to write to the same spot on the screen each time so there’s no scrolling.

By default, the Write-Host line will “flash” by alternating the background color. Or you can use the -FullScreen parameter which will clear the host everytime. If you use this option in your script, make sure the main part of your script is saving data somewhere because you won’t see it. Here’s an example of how you might use it.

After the main portion of the script completes, the flashing message is displayed after the results. If you want to use the fullscreen approach, you could try something like this:

When the main portion of the script finishes you’ll get a flashing screen with the text message. Press any key and you’ll get the results.

I have to say I’m intrigued by this function and can already think of some ways to improve it. If you have suggestions or find this useful, I hope you’ll let me know.

Press PowerShell Pause to Continue

talkbubble Everyone once in a while I come across a PowerShell script that behaves like an old-fashioned batch file. Not that there’s anything wrong with that, but often these types of scripts put in a pause at the end of script so you can see that it finished. You might have seen a command like this in a PowerShell script.

Sure, it works but is hardly elegant PowerShell. First, I’m not sure you even need it. If you want to draw attention to the fact that your script ended, consider using Write-Host with a foreground (or background color).

But if you truly need the user to acknowledge that the script has finished you can use Read-Host.

I’m piping the command to Out-Null because I don’t care what the user enters, only that they hit a key.

If you prefer a move visual cue, you can always resort to the Wscript.Shell COM object.

The popup will remain until the user clicks OK. Or you can change the second parameter from 0 to another value to have the popup automatically dismiss after the specified number of seconds.

So if you need a refreshing pause, there are plenty of PowerShell options for you to use.

After someone posted a comment I realized there is yet another way to do this. Add these two lines to the end of your PowerShell script.

This is very similar behavior to Read-Host but with a bit more flexibility.