Windows Tip Sheet

Taking Title

Update info in a command window's title bar with this handy trick.

Here's another tip on moving from CMD to PowerShell. Back in the day you could use the Title command to change the title bar of the command prompt window. This was very handy if you were executing a long-running batch file. You could periodically update the title bar, minimize the window and still keep track of the script's progress. Today, my long-running script could very well be in PowerShell. Unfortunately, it doesn't support the old Title command.

Instead, I created my own title function and alias, which I store in my PowerShell profile:

function Set-Title {
Param ([string]$NewTitle)
$host.ui.rawui.set_WindowTitle($NewTitle)
}

Set-Alias title Set-Title

The Set-Title function, which has the alias of "title," takes as a parameter the text I want for the window's title bar. Any text with spaces I need to enclose in double quotes. Functionally (no pun intended), my title function works the same as the old title command. However, PowerShell let's me take it a step further.

In CMD there was no way to save or restore the original title text. In PowerShell, I can query the WindowTitle property and save it to a variable. I've added this function as well to my profile:

function Save-Title {
$Global:SavedTitle=$host.ui.rawui.WindowTitle
}

If I know I'm going to be changing title, I'll call Save-Title first to store the current title text. When I'm finished with my code, I can call Set-Title $SavedTitle, and my PowerShell session title is restored.

Assuming you have the above functions in your profile or at the beginning of this script, here's an example of how they can be used:

#RunReport.ps1
Save-Title
$report="e:\logs\report.txt"

"REPORT CREATED "+(get-date).ToString() | Out-File $report

foreach ($server in @(Get-Content s:\servers.txt)) {
#skip blank lines
  if (($server).length -gt 0)
  {
  $newtitle="Checking " + $server.ToUpper()
    title $newtitle
    $server.ToUpper() | Out-File $report -append
    "PageFile" | Out-File $report -append
    Get-WmiObject win32_pagefile -computer $server.Trim() '
    | Out-File $report -append
    "OS" | Out-File $report -append
    Get-WmiObject win32_operatingsystem -computer '
    $server.Trim() | Out-File $report -append
    "ComputerSystem" | Out-File $report -append
    Get-WmiObject win32_computersystem -computer '
    $server.Trim() | Out-File $report -append
      #pause for a few seconds just to show title in action
      #not really required.
      sleep 5
    }
  }

#revert back to old title
Title $savedtitle
#view report
Notepad $report

Tech Help—Just An
E-Mail Away

Got a Windows, Exchange or virtualization question or need troubleshooting help? Or maybe you want a better explanation than provided in the manuals? Describe your dilemma in an e-mail to the MCPmag.com editors at [email protected]; the best questions get answered in this column and garner the questioner with a nifty Redmond T-shirt.

When you send your questions, please include your full first and last name, location, certifications (if any) with your message. (If you prefer to remain anonymous, specify this in your message, but submit the requested information for verification purposes.)

I don't have space to get into the details of this script (these tips are supposed to be quick, remember?). But it reads through the list of servers in s:\servers.txt. It sets the title to indicate what server the script is checking, gets some information from WMI and saves the data to a text file. When finished it opens the report in Notepad.

The script itself isn't anything special; it's merely a vehicle to demonstrate how you can take title of PowerShell.

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