Skip to content

Instantly share code, notes, and snippets.

@RiceBen
Last active July 2, 2021 15:51
Show Gist options
  • Save RiceBen/c28605bd2f5909b16a4869e588aecd77 to your computer and use it in GitHub Desktop.
Save RiceBen/c28605bd2f5909b16a4869e588aecd77 to your computer and use it in GitHub Desktop.
PowerShell 7.x Prompt with posh-git
#Import posh-git module
Import-Module #'{your path}\posh-git.psd1'
function prompt {
#posh-git settings
$GitPromptSettings.DefaultPromptAbbreviateHomeDirectory = $true
$prompt += & $GitPromptScriptBlock
#Assign Windows Title Text
$host.ui.RawUI.WindowTitle = "Current Folder: $pwd"
#Configure current user, current folder and date outputs
$CmdPromptCurrentFolder = Split-Path -Path $pwd -Leaf
$CmdPromptUser = [Security.Principal.WindowsIdentity]::GetCurrent();
$UtcDate = (Get-Date).ToUniversalTime().ToString('HH:mm:ss tt')
# Test for Admin / Elevated
$IsAdmin = (New-Object Security.Principal.WindowsPrincipal ([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
#Calculate execution time of last cmd and convert to milliseconds, seconds or minutes
$LastCommand = Get-History -Count 1
if ($lastCommand) { $RunTime = ($lastCommand.EndExecutionTime - $lastCommand.StartExecutionTime).TotalSeconds }
if ($RunTime -ge 60) {
$ts = [timespan]::fromseconds($RunTime)
$min, $sec = ($ts.ToString("mm\:ss")).Split(":")
$ElapsedTime = -join ($min, " min ", $sec, " sec")
}
else {
$ElapsedTime = [math]::Round(($RunTime), 2)
$ElapsedTime = -join (($ElapsedTime.ToString()), " sec")
}
#Decorate the CMD Prompt
Write-Host ""
Write-host ($(if ($IsAdmin) { 'Elevated ' } else { '' })) -BackgroundColor DarkRed -ForegroundColor White -NoNewline
Write-Host " USER:$($CmdPromptUser.Name.split("\")[1]) " -BackgroundColor DarkBlue -ForegroundColor White -NoNewline
If ($CmdPromptCurrentFolder -like "*:*") {
Write-Host " $CmdPromptCurrentFolder " -ForegroundColor White -BackgroundColor DarkGray -NoNewline
} else {
Write-Host ".\$CmdPromptCurrentFolder\ " -ForegroundColor White -BackgroundColor DarkGray -NoNewline
}
Write-Host " $UtcDate [$elapsedTime] (「・ω・)「" -ForegroundColor White
if ($prompt) {
Write-Host ""$prompt"" -NoNewline
}
return " "
} #end prompt function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment