Windows PowerShell: changing the command prompt

WindowsPowershell

Windows Problem Overview


Using Windows PowerShell, how do I change the command prompt?

For example, the default prompt says

PS C:\Documents and Settings\govendes\My Documents>

I want to customize that string.

Windows Solutions


Solution 1 - Windows

Just put the function prompt in your PowerShell profile (notepad $PROFILE), e.g.:

function prompt {"PS: $(get-date)>"}

or colored:

function prompt
{
    Write-Host ("PS " + $(get-date) +">") -nonewline -foregroundcolor White
    return " "
}

Solution 2 - Windows

Related to a comment to Ocaso Protal's answer, the following is needed for Windows Server 2012 as well as Windows 7 (in a PowerShell window):

new-item -itemtype file -path $profile -force
notepad $PROFILE

I would suggest the following as a prompt if you run with multiple user names (e.g. yourself + a production login):

function Global:prompt {"PS [$Env:username]$PWD`n>"} 

(Credit goes to David I. McIntosh for this one.)

Solution 3 - Windows

At the prompt, I like a current timestamp and resolved drive letters for network drives. To make it more readable, I put it in two lines, and played a bit with colors.

With CMD, I ended up with

PROMPT=$E[33m$D$T$H$H$H$S$E[37m$M$_$E[1m$P$G

For PowerShell, I got the same result with:

function prompt {
    $dateTime = get-date -Format "dd.MM.yyyy HH:mm:ss"
    $currentDirectory = $(Get-Location)
    $UncRoot = $currentDirectory.Drive.DisplayRoot

    write-host "$dateTime" -NoNewline -ForegroundColor White
    write-host " $UncRoot" -ForegroundColor Gray
    # Convert-Path needed for pure UNC-locations
    write-host "PS $(Convert-Path $currentDirectory)>" -NoNewline -ForegroundColor Yellow
    return " "
}

Which is a little more readable :-)

BTW:

  • I prefer powershell_ise.exe $PROFILE instead of (dumb) Notepad.

  • If you like to debug your prompt() with breakpoints, you should rename the prompt-function to anything else (or try it in another file). Otherwise you might end up in a loop: When you stop debugging, prompt() is called again and you stop at the breakpoint, again. Quite irritating, at first...

Solution 4 - Windows

If you want to do it yourself, then Ocaso Protal's answer is the way to go. But if you're lazy like me and just want something to do it for you, then I highly recommend Luke Sampson's Pshazz package.

Just to show you how lazy you can be, I'll provide a quick tutorial.

  • Install Pshazz with Scoop (scoop install pshazz)
  • Use a nice predefined theme (pshazz use msys)
  • Drink (root) beer

Pshazz also allows you to create your own themes, which is as simple as configuring a JSON file. Check out mine to see how easy it is!

Solution 5 - Windows

To just show the drive letter I use:

function prompt {(get-location).drive.name+"\...>"}

Then to revert to the path I use:

function prompt {"$pwd>"}

Solution 6 - Windows

This version of Warren Stevens' answer avoids the noisy "Microsoft.PowerShell.Core\FileSystem" in the path if you Set-Location to network shares.

function prompt {"PS [$Env:username@$Env:computername]$($PWD.ProviderPath)`n> "} 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsashangView Question on Stackoverflow
Solution 1 - WindowsOcaso ProtalView Answer on Stackoverflow
Solution 2 - WindowsWarren StevensView Answer on Stackoverflow
Solution 3 - WindowsTrivia CoderView Answer on Stackoverflow
Solution 4 - WindowsFomentiaView Answer on Stackoverflow
Solution 5 - WindowsChris HoranView Answer on Stackoverflow
Solution 6 - WindowsnudlView Answer on Stackoverflow