Skip to content

Instantly share code, notes, and snippets.

@DeadlyBrad42
Last active June 1, 2023 17:03
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DeadlyBrad42/ab69aedcd9741df3151b to your computer and use it in GitHub Desktop.
Save DeadlyBrad42/ab69aedcd9741df3151b to your computer and use it in GitHub Desktop.
Cool Powershell Prompt

Custom Powershell Prompt

Inspired by the fancy ones all your *nix folks use

The easiest way to install it is to notepad $PROFILE and copy the script in. The Prompt function that we just added will be run on the next startup of Powershell. The colors I'm using here are part of the base16 color scheme.

Features

  • I used the big blue ribbon to help find my last command after I execute something wall-of-texty
  • There's an additional info display when you're in a git directory. It shows the branch name, if the branch has uncommitted changes (the green dot), and if there's pulls or pushes avilable.
  • A small symbol will appear next to your name if you're running in Administrator mode

Issues

  • The information display for git is kind of slow right now because it's running 3 git commands to figure it out. I need to dig a bit more into git commands and regex-fu to make that a bit faster
  • I thought about adding addtional information displays, but my mind was drawing a blank... :/

License

Modify it all you want! Feel free to send me your changes, I'm always interested in seeing improvements to it. If there's any questions, just ask!

function prompt
{
# Determine if Powershell is running as Administrator
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal( [Security.Principal.WindowsIdentity]::GetCurrent() )
$isAdmin = $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
$leftCharCount = 0
$middleCharCount = 0
$rightCharCount = 0
# Grab current git branch
$isGitRepo = ""
if(Test-Path .git) {
$isGitRepo = "yeah"
}
# Grab current loaction
$location = $(get-location).Path;
Write-Host ("")
Write-Host (" [ ") -nonewline -foregroundcolor White -backgroundcolor DarkBlue
$leftCharCount += 3
Write-Host ([Environment]::UserName) -nonewline -foregroundcolor White -backgroundcolor DarkBlue
$leftCharCount += [Environment]::UserName.length
if ($isAdmin) {
Write-Host ("†") -nonewline -foregroundcolor DarkYellow -backgroundcolor DarkBlue
$leftCharCount += 1
}
Write-Host (" @") -nonewline -foregroundcolor Gray -backgroundcolor DarkBlue
$leftCharCount += 2
Write-Host ([System.Net.Dns]::GetHostName()) -nonewline -foregroundcolor White -backgroundcolor DarkBlue
$leftCharCount += [System.Net.Dns]::GetHostName().length
Write-Host (" ] ") -nonewline -foregroundcolor White -backgroundcolor DarkBlue
$leftCharCount += 3
Write-Host ("▓▓▒▒░░") -nonewline
$leftCharCount += 6
if ($isGitRepo) {
# Grab current branch
$git_branchName = "";
git branch | foreach {
if ($_ -match "^\* (.*)") {
$git_branchName += $matches[1]
}
}
# Check if workspace has changes
$git_changes = 0
$git_changesDisplay = ""
git status --porcelain | foreach {
$git_changes++
}
if ($git_changes -gt 0) {
$git_changesDisplay = "•"
}
# Check if pushes or pulls available
$git_pushes = ""
$git_pulls = ""
git status -sb | foreach {
if ($_ -match "ahead (\d+)") {
$git_pushes = " ↑" + $matches[1]
}
if ($_ -match "behind (\d+)") {
$git_pulls = " ↓" + $matches[1]
}
}
# Calculate length of git display (by making a new string)
$rightCharCount = "[ Ѱ$($git_changesDisplay) $($git_branchName)$($git_pushes)$($git_pulls) ] ".length
# Write spaces for padding, so that the display is right-aligned
$middleCharCount = $ui.WindowSize.Width - $leftCharCount - $rightCharCount
for ($i=1; $i -le $middleCharCount; $i++)
{
Write-Host (" ") -nonewline
}
# Actually output the git display
Write-Host ("[ ") -nonewline
Write-Host ("Ѱ") -nonewline -foregroundcolor DarkGray
Write-Host ($git_changesDisplay) -nonewline -foregroundcolor DarkGreen
Write-Host (" $git_branchName") -nonewline
Write-Host ("$git_pushes") -nonewline -foregroundColor Green
Write-Host ("$git_pulls") -nonewline -foregroundColor Red
Write-Host (" ] ") -nonewline
} else {
# No alternate display, just send a newline
Write-Host ("")
}
Write-Host ("»") -nonewline -foregroundcolor DarkGreen
if ($location -eq "C:\Users\$([Environment]::UserName)") {
Write-Host ("~\") -nonewline
} else {
Write-Host ($location) -nonewline
}
Write-Host ("»") -nonewline -foregroundcolor DarkGreen
return " "
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment