Weekend Scripter: Playing with PowerShell Prompt

Doctor Scripto

Summary: Microsoft Scripting Guy, Ed Wilson, talks about playing with the Windows PowerShell prompt.

Microsoft Scripting Guy, Ed Wilson, is here. It is time to round up all of those end-of-the-year kinds of things. This is, in fact, the last weekend Hey, Scripting Guy! Blog post of 2014. Not sure what that means; but it is true, this is the last weekend post of the year. Not that I am going to do anything special, or make a big deal of it, but I just was thinking about it.

One thing I like to do from time-to-time, is override my Windows PowerShell prompt. The cool thing is that I can do it on the fly. This is because it is simply a function—and like all functions, I can overwrite the behavior.

Like all other Windows PowerShell functions, the Windows PowerShell prompt resides on the Function drive. I can use the Get-Content cmdlet to retrieve the current value. Here is the one on my computer:

PS C:\Users\ed> Get-Content Function:\prompt

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

# .Link

# http://go.microsoft.com/fwlink/?LinkID=225750

# .ExternalHelp System.Management.Automation.dll-help.xml

OK, it is a little shaggy. The main thing, in fact, the only executing code is a one-liner. It retrieves the current location, and it displays a right arrow if I am in a nested prompt. It is basic and generally does what I want. It also lets me have pretty much all the space at the Windows PowerShell console that I could possibly want.

But, it can be shorter. In fact, I know quite a few people who omit the initial PS. Here is a prompt that does that:

Function prompt {"$($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "}

All I need to do is to type the command at the Windows PowerShell console, and press ENTER. Here is the command and its results:

Image of command output

When I close and re-open the Windows PowerShell console, my Windows PowerShell console prompt returns to the default value.

But suppose that I do not care what my current location is. In that case, I can retrieve a few more spaces on the Windows PowerShell console line. Here is an example of that:

Function prompt {"$('>' * ($nestedPromptLevel + 1)) "}

The prompt is shown here:

Image of command output

In each case, I use the content of the previous function to create my new prompt function. But I do not need to do that. I can, for example, create a prompt that is totally mine. Here is an example that displays the current date:

Function prompt {"HSG $([datetime]::Now.ToShortDateString()): "}

Here is the prompt:

Image of command output

One thing that might be fun to do is change the background color of the Windows PowerShell console—on a random basis. Here is a prompt that does that:

Function prompt {[console]::backgroundcolor = $(Get-Random -Minimum 0 -Maximum 15); "HSG $([datetime]::Now.ToShortDateString()): "}

Keep in mind that not all colors of a background color will be compatible with the foreground color, so you might want to add a bit of logic if you really want random colors. Each time a Windows PowerShell command runs, the background color changes, but the foreground color is currently remaining the same. Here is what that might look like:

Image of command output

Remember that if things don’t work out, all you need to do is close Windows PowerShell and re-open it. This is because you are creating a temporary function that is only stored in memory. I can redefine it at will and on-the-fly. If I come up with something I really like, I can add it to my Windows PowerShell profile. I will talk about that later.

For now, have fun, and let me know what crazy ideas you come up with.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy


Discussion is closed.

Feedback usabilla icon