Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Look at Me!

Posted on August 12, 2014August 10, 2014

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.

Function Invoke-Flasher {

<#
.Synopsis
Display a flashing message.

.Description
This command will present a flashing message that you can use at the end of a script or command to signal the user. By default the command will write a message to the console that alternates the background color between the current console color and a red. You can press any key to end the function and restore the original background color.

You can also use the -FullScreen switch which will alternate the background color of the entire PowerShell console. Be aware that you will lose any script output that was displayed on the screen.

This command will NOT work in the PowerShell ISE.
.Example
PS C:\> Get-Process | Sort WS -Descending | select -first 5 ; Invoke-Flasher

This is a command line example of what a basic script would look like. 
.Example
PS C:\> $data = get-eventlog Security ; Invoke-Flasher "Security logs retrieved." -fullscreen ; $data

This example uses the fullscreen parameter because the command output was saved to a variable.
.Notes
Last Updated: August 10, 2014
Version     : 0.9


Learn more:
 PowerShell in Depth: An Administrator's Guide (http://www.manning.com/jones2/)
 PowerShell Deep Dives (http://manning.com/hicks/)
 Learn PowerShell in a Month of Lunches (http://manning.com/jones3/)
 Learn PowerShell Toolmaking in a Month of Lunches (http://manning.com/jones4/)


  ****************************************************************
  * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED *
  * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK.  IF   *
  * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, *
  * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING.             *
  ****************************************************************

.Link
https://jdhitsolutions.com/blog/2014/08/look-at-me

.Link
Write-Host

.Inputs
None

.Outputs
None

#>

[cmdletbinding()]

Param(
[Parameter(Position=0)]
[string]$Text="The command has completed.",
[string]$Color = "red",
[Switch]$FullScreen
)

#save current background color
$bg = $host.ui.RawUI.BackgroundColor

$Running = $True

#set cursor position
$Coordinate = $host.ui.RawUI.CursorPosition

While ($Running) {

    if ($host.ui.RawUI.BackgroundColor -eq $bg) {
        $host.ui.RawUI.BackgroundColor = $color
        if ($FullScreen) { Clear-Host}
    }
    else {
        $host.ui.RawUI.BackgroundColor = $bg
       if ($FullScreen) { Clear-Host}
    }

    #set the cursor position
    $host.ui.rawui.CursorPosition=$Coordinate
    Write-Host "`n$Text Press any key to continue . . ."
    
    #see if a key has been pushed
    if ($host.ui.RawUi.KeyAvailable) {
        $key = $host.ui.RawUI.ReadKey("NoEcho,IncludeKeyUp,IncludeKeyDown")
        if ($key) {
          $Running = $False
        }
    } #if key available
      start-sleep -Milliseconds 500

} #while


$host.ui.RawUI.BackgroundColor = $bg

if ($FullScreen) { Clear-Host}

}

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.

#dot source the function
. c:\scripts\Invoke-Flasher.ps1

cls
$computers = get-content computers.txt

$data =  Get-Process -computername $Computers | group Machinename -AsHashTable
$data.GetEnumerator() | foreach { $_.value | sort WS -Descending | Select -first 5 | format-table -group Machinename}

Invoke-Flasher

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:

#dot source the function
. c:\scripts\Invoke-Flasher.ps1

cls
$computers = get-content computers.txt

$data =  Get-Process -computername $Computers | group Machinename -AsHashTable
$output = $data.GetEnumerator() | foreach { $_.value | sort WS -Descending | Select -first 5 | format-table -group Machinename}

Invoke-Flasher -fullscreen

$output

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.

Share this:

  • Print (Opens in new window) Print

Like this:

Like Loading…

Related

1 thought on “Look at Me!”

  1. Pingback: BPOTW 2014-08-15 | SQL Notes From The Underground

Comments are closed.

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2026 The Lonely Administrator | Powered by SuperbThemes!
%d