Search code examples
powershellprompt

How do I customize my PowerShell prompt (colour and text)?


I'm running PowerShell 7.0.0 on Windows 10, and would like to customize my command prompt so it looks like the following:

Username@Hostname YYYY-MM-DD HH:MM:SS:PresentWorkingDirectory>

with the following colors:

username: green

hostname: blue

YYYY-MM-DD HH:MM:SS: purple

:: white

PresentWorkingDirectory: blue

>: white

Question:

What is the requisite profile code to make the above happen? So far I know that Get-Date -UFormat '%Y-%m-%d %H:%M:%S' returns the YYYY-MM-DD HH:MM:SS I want


Solution

  • This will do it:

    function prompt {
        Write-Host $env:USERNAME -ForegroundColor Green -NoNewline
        Write-Host "@" -NoNewline
        Write-Host $env:COMPUTERNAME -ForegroundColor Blue -NoNewline
        Write-Host " $((Get-Date).toString('yyyy-MM-dd hh:mm:ss'))" -ForegroundColor DarkMagenta -NoNewline 
        Write-Host ":" -NoNewline
        Write-Host $($executionContext.SessionState.Path.CurrentLocation) -ForegroundColor Blue -NoNewline 
        "$('>' * ($nestedPromptLevel + 1)) "
    }
    

    I hope you have enough screens to span your terminal over it ;-)

    Learn more about PowerShell prompt customization