Quick tip: Seamlessly switch between different prompts

Most users are probably happy with the default PowerShell prompt. But there are a handful of people that are not satisfied with any product unless they have had a chance to customize it to their needs. Luckily PowerShell supports defining your own prompt quite easily. Then there are another few that for some reason likes to have multiple prompts that they alternate between. Perhaps they have one for home, one for work and another one when they are presenting at usergroups and conferences.

In this blog post I will present the way I do this, and hopefully there are others like me out there that also might find it useful.

First off, I have a custom function that I have defined in my PowerShell profile that lets me quickly switch between different prompts.

function Set-Prompt {
    param ([ScriptBlock] $ScriptBlock)
    $null = New-Item -Path function:prompt -Value $ScriptBlock -Force
}

Then I first save the original prompt like this:

$originalPrompt = (Get-Item function:prompt).ScriptBlock

After that I can define as many different prompts I like.

$customPrompt = {
    # my custom prompt!
}

$demoPrompt = {
    # my demo prompt!
}

$fancyPrompt = {
    # my fancy prompt!
}

These all go into my profile, so that I always have them available for when I need to quickly change my prompt, which I can do like this:

Set-Prompt $fancyPrompt
# Now I have my fancy prompt

Set-Prompt $demoPrompt
# And quickly change to my demo prompt

Set-Prompt $originalPrompt
# Before I'm back to the original PowerShell prompt

I hope this little tip have been helpful. If you want to share your favorite PowerShell prompt with me, please do so in the comments section below.

Until next time!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s