Weekend Scripter: Customize PowerShell Title and Prompt

Summary: Learn how to interactively update the Windows PowerShell prompt and title.

Honorary Scripting Guy, Sean Kearney, is here today to have a little bit of fun with the Windows PowerShell console. Why not? It’s a Sunday, it’s the last day of my time off, and I feel like playing.

There have been many posts online about the fun you can have with the $Host.UI.RawUI object in Windows PowerShell. You can use it to edit cursor locations, flip colors about…almost anything your little heart desires.

Another fun little thing we’re going to play with is the Windows PowerShell prompt. Yes! You’re allowed to mess around with this. It’s completely OK because we’re going to make this change through our personal PowerShell profile.

This means if we decide we don’t like what we did, we can remove it from the $Profile file and everything will revert to the default.

For the first fun piece, were going to change the title on the PowerShell bar. It’s actually very easy to read and write to the $Host.UI.RawUI object. If you’d like to see the current title, simply access the WindowTitle property:


If you’d like to have some fun, with one line, you can add the Time and Date to the current title:


Sure, neat trick. But this only updates once, which is neat, but not really useful.

I suppose I could find some funky cool way to play with events and triggering, but how about a simpler approach? How about each time I hit ENTER in the console, the Date and Time are updated? It wouldn’t be quite “real time,” but it would work. This is where we get to play with the PowerShell prompt.

The prompt is actually a very simple PowerShell function. The neat thing is that you can edit this function. To pull up the properties of this function, access the ScriptBlock parameter:

(Get-Command Prompt).scriptblock

Here is the default code for the prompt in Windows PowerShell. If you look at it, it’s a just simple script that displays a line of text:

"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "
# .Link
# http://go.microsoft.com/fwlink/?LinkID=225750
# .ExternalHelp System.Management.Automation.dll-help.xml

Hmmm…An ongoing script, eh? I could have a lot of fun with this!

I could even have the prompt beep every time I hit ENTER. I just need to redefine the function. To ensure that I maintain anything Microsoft wanted to be in there, I use the original code as a base at all times. Here we’ve added a single line to make it beep—just to be irritating:

Function Prompt
"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "
# .Link# http://go.microsoft.com/fwlink/?LinkID=225750
# .ExternalHelp System.Management.Automation.dll-help.xml

Isn’t that kinda neat? Because this simply a function (albeit a very special function, which is called every time we hit ENTER in the console), we could use this to update our title bar. But we could do so much more. This is the line that displays the current folder and path:

"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "

I can actually capture this data and use it in my title bar. The reason you might want to do this? Sometimes the path can get pretty deep, and it eats up half the desktop real estate in the console. But the information is relevant, so I don’t want to lose it.

First I’ll capture the current prompt data:

$PromptData="PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "

Now I’ll update the title bar with the information from the prompt and the current date and time. I add a little hyphen to make it more readable:


If I don’t do anything more, Windows PowerShell will default to a prompt of PS.

Now all I need to do is add this block of code to my PowerShell profile. Here’s the updated function:

Function Prompt
$PromptData="PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "
# .Link# http://go.microsoft.com/fwlink/?LinkID=225750
# .ExternalHelp System.Management.Automation.dll-help.xml

When I run this in my current PowerShell console, I get cool results like this!

Image of title bar

This is a fun example, but can you imagine the fun things you could do with the prompt and the title now? Let your imagination be your guide!

I invite you to follow the Scripting Guys on Twitter and Facebook. If you have any questions, send email to them at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, always remember that with great PowerShell comes great responsibility.

Sean Kearney, Honorary Scripting Guy, Cloud and Datacenter Management MVP

Comments (3)

  1. Mike Shepard says:

    That’s a nice solution to the "the path is so long there’s no room on the line for commands". 🙂

  2. tommymaynard says:

    Thanks for your post, Sean. I’m glad to see others taking advantage of the PowerShell prompt! I took advantage of the prompt function to report on background jobs. Part I starts on PowerShell.org (http://powershell.org/wp/2015/10/12/run-background-commands-after-every-command).
    It’ll get you to part II where I added indicators to the title to tell me if there were more than one background job (Job vs. Jobs), to display an asterisk if a job was still running, and to display a plus sign if a job has more data (hasn’t yet been received).
    My problem was that I didn’t want to close a console if there was a background job. I considered the path-to-title option, but it seemed like too much work (that sounds ridiculous, I know), to look up at the title bar so frequently. Maybe I should have included
    an option to only write it to the title if it’s over a set amount of characters. Anyway, thanks again, Sean.

  3. @Mike Shepard

    Thanks! It was just a fun thought that could turn out to be useful.


    Glad I was able to inspire some answers ! That was a pretty cool solution you had there!

    Honorary Scripting Guy

Skip to main content