×
Search results provided by Azure Search - read how I built it in this post.
Max Melcher

3 minute read

Adjust the default prompt in PowerShell is possible: In this short post I show you how and share my new prompt!

Twitter Serendipity

I saw this thread on Twitter by some smart PowerShell folks:

… and immediately thought that this would be EXTREMLY #awesomesauce to have.

I am grateful that at least two of them, namely Ryan Yates and Amanda Debler shared their prompt function here and here - and I mixed both of them and came up my version.

Background - The Prompt Function

If you open a PowerShell session and type in a command, every time you issue it, the Prompt() function defined in the path C:\Users[USERNAME]\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 is executed. How cool is that. A prompt override!

So if you want to edit your Prompt() function you can open a new PowerShell session and type code $PROFILE or if you dont have VSCode (?!) installed notepad $PROFILE to open the file.

My Prompt Function

So combining the two gists from Amanda and Ryan, my Prompt() function has three core features:

  1. Only the last two folders and the root drive is shown in the current path.
    1. The path c:\very\deep\path\really\really\deep becomes to c:...\really\deep
  2. The execution time is shown
    1. Colored in green if the execution time is below 1 second; milliseconds are shown.
    2. Yellow if the time is below a minute; seconds are shown.
    3. Red if the time is above a minute; hours, minutes and seconds are shown. If you have a script that runs longer than a day (really?!) then you might have to fix that.
  3. If you are in a path that is a git repository, the branch and the state is shown.
    1. You need Posh-Git installed - with chocolatey its choco install poshgit.

And all the features in one screenshot:

And it even works in VSCode.

Awesome, right?

Give me the code

And here comes my function - feel free to adjust to your needs:
Note the posh-git path at the very end!

<#
.Description
Custom Prompt() function with 3 features: 
1. Show execution times for commands
2. Reduce path length to show only 2 parent folders
3. Show git status if the folder is under source control

.Kudos
As mentioned in the post, I mixed the script based on existing scripts by 
@ryanyates1990, @texmandie, @fatherjack, @FredWeinmann, @psdbatools, @cl

#>
function Prompt {
    
    try {        
        $history = Get-History -ErrorAction Ignore -Count 1
        if ($history) {
            Write-Host "[" -NoNewline
            $ts = New-TimeSpan $history.StartExecutionTime $history.EndExecutionTime
            switch ($ts) {
                {$_.TotalSeconds -lt 1} { 
                    [int]$d = $_.TotalMilliseconds
                    '{0}ms' -f ($d) | Write-Host -ForegroundColor Black -NoNewline -BackgroundColor DarkGreen
                    break
                }
                {$_.totalminutes -lt 1} { 
                    [int]$d = $_.TotalSeconds
                    '{0}s' -f ($d) | Write-Host -ForegroundColor Black -NoNewline -BackgroundColor DarkYellow
                    break
                }
                {$_.totalminutes -ge 1} { 
                    "{0:HH:mm:ss}" -f ([datetime]$ts.Ticks) | Write-Host -ForegroundColor Gray -NoNewline  -BackgroundColor Red
                    break
                }
            }
            Write-Host "] " -NoNewline
        }
        if(Get-Module Posh-git) {
            Write-VcsStatus
            Write-Host " " -NoNewline
        }
    }
    catch { }
    # New line

    Write-Host ""
    
    # show the drive and then last 2 directories of current path

    if (($pwd.Path.Split('\').count -gt 3)){
        write-host "$($pwd.path.split('\')[0], '...', $pwd.path.split('\')[-2], $pwd.path.split('\')[-1] -join ('\'))" -NoNewline
    }
    else{
        Write-Host "$($pwd.path)" -NoNewline
    }
    "> "
}

#Adjust your post-git path! I installed it with chocolatey!

Import-Module 'C:\tools\poshgit\dahlbyk-posh-git-9bda399\src\posh-git.psd1'

The only “disadvantage” I noticed is, that the initial load of the PowerShell is delayed by 1 second. I guess thats because the post-git module is loaded. That is acceptable.

I thought about moving the path and the execution time to the PowerShell console title - but that would not work in VSCode so I left it. Still liking the idea, maybe it is something for you.

Hope it helps,
Max

comments powered by Disqus