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.
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.
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.
Jeff – changing prompts was one of the very reasons I originally got excited about using dos command prompt back in the day. Fun stuff!
Search my blog for “prompts” and you’ll find a few more posts on the subject with other examples.
See http://gallery.technet.microsoft.com/scriptcenter/f7204f78-e73b-468c-8d8f-ceeaaae65aee for a function that takes the padding idea to another level.