Windows Tip Sheet

Switching Days in PowerShell

What am I scripting and why am I here? It's quitting time, according to my prompts.

In previous columns I showed how to customize your CMD and PowerShell prompts. My CMD prompt looks like this:

[Thu 01/18/2007 13:18:35.47] E:\temp>

and my PowerShell prompt, this:

1/18/2007 9:24:42 PM PS C:\ >

One thing missing from the PowerShell prompt is the day of the week. The Get-Date cmdlet that I use in my prompt will give me the day of the week, but not as an abbreviation. And since I'm already pushing my limit on console space, I didn't add it. But it nagged at me. The answer was easier than I realized.

Here is my revised prompt that now looks like this:

Thu 1/18/2007 9:24:42 PM PS C:\ >

Here's the function:

function prompt
{
Switch ((get-date).dayofweek.toString()) {
Sunday {$dow="Sun";break}
Monday {$dow="Mon";break}
Tuesday {$dow="Tue";break}
Wednesday {$dow="Wed";break}
Thursday {$dow="Thu";break}
Friday {$dow="Fri";break}
Saturday {$dow="Sat";break}

}

$myPrompt=$dow+ " " + (get-date).ToShortDateString() + `
" " + (get-date).ToLongTimeString()+ " PS " + `
$(get-location) + " >"
   Write-Host ($myPrompt) -nonewline -foregroundcolor Yellow
   return " "
}

The magic is using the Switch statement. If you know VBScript, think of Switch as a PowerShell version of Select Case. Switch takes a variable and evaluates it. In my function, it evaluates the result of this expression:

((get-date).dayofweek.toString())

The value will be the full day of the week like Thursday. The rest of the Switch expression looks for matches and, for each match, executes the code in brackets. So if today is Thursday, this code runs:

{$dow="Thu";break}

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've created a new variable with a value of Thu. The Break command tells Switch to stop evaluating. All that's left is to concatenate my new prompt and include $dow:

$myPrompt=$dow+ " " + (get-date).ToShortDateString() + `
" " + (get-date).ToLongTimeString()+ " PS " + `
$(get-location) + " >"

Now my prompts and pretty much identical and I'll always know when it's quitting time.

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