Prof. Powershell

Belly Up to the Title Bar

Jeffery Hicks walks you through creating status messages through the PowerShell console.

When you write a PowerShell script or function, sometimes it is helpful to provide feedback or status messages to the person running the script. Even if it is just for yourself, it is helpful to know what the script is doing. There are a number of ways you can accomplish this such as using Write-Host, Write-Progress or Write-Verbose. But here's another way using the title bar of the PowerShell console or ISE.

You can get and set the window title directly from PowerShell:

PS C:\> $host.ui.rawui.WindowTitle
Windows PowerShell 4.0

I'm going to walk you through a sample script that uses this technique. It will work for both the PowerShell console and the PowerShell ISE. First, you will want to save the current title so that you can set it back when you are finished.

$title = $host.ui.RawUI.WindowTitle

My script is going to analyze all of the files in a given path and show the total size of each group of files by extension. I need a path.

$path = "c:\scripts"

Since this is the beginning of my script I am going to go ahead and update the status using the window title.

$status = "Processing $path"
$host.ui.RawUI.WindowTitle = $status

Now, to get all the files. The following syntax requires PowerShell 3.0 or later and will skip any files without file extensions.

$files = dir -Path $path -File -Recurse | where extension

Next, I am going to group the files by extension so I'll update the status.

$status = "Grouping data"
$host.ui.RawUI.WindowTitle = $status
$grouped = $files | group -Property Extension

Now to measure each group of files, again updating the status.

$status = "Measuring data"
$host.ui.RawUI.WindowTitle = $status

For the sake of my demonstration I'm going to use ForEach and process each file group. I'll update the status for each group. I also added an arbitrary sleep interval so you can see the title bar change.

$data = foreach ($group in $grouped) {
$status = "Processing $($group.name)"
$host.ui.RawUI.WindowTitle = $status
$group | Select Name,Count,
@{Name="SizeKB";
Expression={[math]::Round(($_.group | measure length -sum).sum/1kb,2)}}
Start-sleep -Milliseconds 200
}

Almost finished. I send the results to Out-Gridview, again updating the status via the window title.

$status = "Sending to Out-Gridview"
$host.ui.RawUI.WindowTitle = $status
$data | sort SizeKB -Descending | Out-GridView -Title "File Report: $path"

All that remains is to restore the original window title using the variable I set at the beginning of the script.
$host.ui.RawUI.WindowTitle = $title

Here is a video clip of this technique in action.

What I like about this technique is that I can provide status information without taking up space in the console. There is also no reason you can't combine this with Write-Host or Write-Verbose. Or if you are looking for yet another way to add trace or status messages, take a look at an article I published on my blog a few months ago on using Internet Explorer as a message window.

Next time, I'll show you another way to display status messages.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular