Skip to content

Instantly share code, notes, and snippets.

@wsmelton
Last active September 9, 2020 23:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wsmelton/3809bcb444e228a24ccb9e185dfc202d to your computer and use it in GitHub Desktop.
Save wsmelton/3809bcb444e228a24ccb9e185dfc202d to your computer and use it in GitHub Desktop.
My prompt that is part of my profile for PowerShell
function Prompt {
$major = $PSVersionTable.PSVersion.Major
$minor = $PSVersionTable.PSVersion.Minor
$patch = $PSVersionTable.PSVersion.Patch
if ($major -lt 6) {
Write-Host "[PS $($major).$($minor)] [" -NoNewline
} else {
$patch = $PSVersionTable.PSVersion.Patch
Write-Host "[PS $($major).$($minor).$($patch)] [" -NoNewline
}
Write-Host (Get-Date -Format "HH:mm:ss") -ForegroundColor Gray -NoNewline
try {
$history = Get-History -ErrorAction Ignore -Count 1
if ($history) {
Write-Host "] " -NoNewline
$ts = New-TimeSpan $history.StartExecutionTime $history.EndExecutionTime
switch ($ts) {
{$_.TotalMinutes -ge 1 } {
'[{0,5:f1} m ]' -f $_.TotalMinutes | Write-Host -ForegroundColor DarkRed -NoNewline
}
{$_.TotalMinutes -lt 1 -and $_.TotalSeconds -ge 1} {
'[{0,5:f1} s ]' -f $_.TotalSeconds | Write-Host -ForegroundColor DarkYellow -NoNewline
}
default {
'[{0,5:f1}ms ]' -f $_.Milliseconds | Write-Host -ForegroundColor Cyan -NoNewline
}
}
} else {
Write-Host "] >" -ForegroundColor Gray -NoNewline
}
} catch { }
if (Test-Path .git) {
$branchName = git branch | ForEach-Object { if ( $_ -match "^\*(.*)" ) { $_ -replace "\* ", "" } }
Write-Host "[ git:$branchName ]" -ForegroundColor Yellow -NoNewLine
}
Write-Host " $($executionContext.SessionState.Path.CurrentLocation.ProviderPath)"
"[$($MyInvocation.HistoryId)] > "
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment