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

Friday Fun PowerShell Pep Talk

Posted on April 1, 2011

Today's Friday Fun is meant to help get you excited about the upcoming Scripting Games. I want to add a little pep to your PowerShell prompt. Perhaps it will even keep you motivated. What I have for you today are variety of prompt functions. Consider them variations on a theme.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

Windows PowerShell has a default function called prompt that defines the console prompt. You can see your current prompt definition with a simple command.

[cc lang="Powershell"]
PS C:\> $function:prompt
[/cc]

Today I'm going to give you some new prompt functions that will include a peppy PowerShell message. Normally you would put these commands in your profile script. But you can copy and paste into an open shell. First, let's define an array of our PowerShell slogans.

[cc lang="PowerShell"]
$global:slogans=@(
"PowerShell Rocks!",
"Energize!!",
"To Shell and Back",
"I am the Shell",
"PowerShell to the People",
"Powered by PS",
"PowerShell Rulez!",
"PowerShell Fanboy"
)
[/cc]

I'm using the global modifier to indicate this $slogans is a global variable. You'll get better results if you keep the messages close to the same length. Now we need a function to get a random element from the array. This is accomplished by piping the $slogans array to the Get-Random cmdlet. Here's a basic example.

[cc lang="PowerShell"]
Function Prompt {

#reference the global $slogans array defined and
#get a random element.
"$($global:slogans | Get-Random) $(Get-Location) > "

}
[/cc]

The function writes a string that is a combination of the randomly selected slogan and the current location. You would put this in your profile and start a new PowerShell session. Or you can copy and paste the function into your current session. This will give results like this:

[cc lang="DOS"]
Powered by PS E:\temp > cd c:\work
Energize!! C:\work > ps powershell

Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
942 39 102028 112412 632 13.57 4852 powershell

I am the Shell C:\work >
[/cc]

Sort of ok. But it gets a little "jagged" because the messages are of different lengths. We can adjust this by padding the slogan text. Here's a variation:

[cc lang="PowerShell"]
Function Prompt {
#get length of longest slogan and pad accordingly

#get the longest slogan length
$l=($slogans | sort length)[-1].length
#pad with the . character
"$(($global:slogans | get-random).Padright($l+1,"."))$(Get-Location) > "

}
[/cc]

This function gets the length of the longest entry in array and uses that value in the PadRight() method. In my version I'm padding with the period. Here's the end result:

[cc lang=":DOS"]
PowerShell Rulez!.C:\work > cd hklm:
PowerShell Rocks!.HKLM:\ > cd wsman:
PowerShell Rulez!.WSMan:\ >
PowerShell Fanboy.WSMan:\ > ps powershell

Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
1129 39 102252 112772 632 13.65 4852 powershell

I am the Shell....WSMan:\ >
Energize!!........WSMan:\ >
[/cc]

A little nicer. What about adding some color?

[cc lang="PowerShell"]
Function Prompt {

#get the longest slogan length
$l=($slogans | sort length)[-1].length
#pad with the . character
Write-Host "$(($global:slogans | get-random).Padright($l+1,"."))" -foregroundcolor CYAN -NoNewLine
Write-host "$(Get-Location)" -NoNewline
#the function has to return something
Write "> "

}
[/cc]

The only difference is that I'm using Write-Host to display components of the prompt using the -foreground color parameter.

PowerShell Pep Talk

Finally, how about adding a cheerleader? We'll add this to the profile, or paste into your session to test.

[cc lang="PowerShell"]
#create a global Voice object in your profile
$global:Voice=New-Object -ComObject SAPI.SPVoice
[/cc]

And here is my prompt function that will now "speak" the PowerShell slogan to really get me motivated.

[cc lang="PowerShell"]
Function Prompt {

#get the longest slogan length
$l=($slogans | sort length)[-1].length

#get a slogan
$text=$global:slogans | Get-Random

#pad with spaces
Write-Host "$($text.Padright($l+1," "))" -foregroundcolor CYAN -NoNewLine
Write-host "$(Get-Location)" -NoNewline
#the function has to return something
Write "> "

#speak it
$global:voice.speak($text) | Out-Null

}
[/cc]

I hope you'll give this a shot. And don't worry, assuming you haven't modified your profile, any changes made will go away when you close your session. Open a new session and you will be back at your original prompt. Enjoy!

You can download my code samples here.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

3 thoughts on “Friday Fun PowerShell Pep Talk”

  1. marcadamcarter says:
    April 1, 2011 at 10:51 pm

    Jeff – changing prompts was one of the very reasons I originally got excited about using dos command prompt back in the day. Fun stuff!

    1. Jeffery Hicks says:
      April 2, 2011 at 8:04 am

      Search my blog for “prompts” and you’ll find a few more posts on the subject with other examples.

  2. Jeffery Hicks says:
    April 3, 2011 at 10:58 am

    See http://gallery.technet.microsoft.com/scriptcenter/f7204f78-e73b-468c-8d8f-ceeaaae65aee for a function that takes the padding idea to another level.

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

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

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