Last active
November 24, 2020 16:30
-
-
Save belotn/4b83e76164a62590fa86823bd3d35f67 to your computer and use it in GitHub Desktop.
Custom Prompt Function for powershell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################################ | |
# FUNCTION: Prompt # | |
############################################################ | |
# Redefine Prompt setting new color for new sessions # | |
# and setting username, servername, PID and WS as title # | |
# TODO: Customisable prompt FIXED/ # | |
# TODO: Add Domain Information #D DONE/ # | |
############################################################ | |
function Prompt { | |
<# | |
.Description | |
Customize your prompt s | |
Supported Format | |
#S | |
Shell, ie PS | |
#P | |
Current Path | |
#L | |
Leaf of the current path | |
#E | |
Powershell classique end '>' | |
#C | |
Custom entry : | |
Display content set after : | |
$Var => Standfor content of variable $Var | |
&Var => Standfor content of scriptblock in $Var | |
String string to display | |
example #C:[ | |
#A | |
$ if user ou # if admin | |
#U | |
USerName | |
#M | |
MachineName | |
#I | |
Process ID | |
#V | |
Powershell Version | |
#W | |
WorkingSet Size | |
#G | |
Git Status | |
~Color | |
each TAG can be colorised by appending ~ColorNAme | |
#> | |
if (-not($Prompt)) { | |
$LocalPrompt = "#S #P#E" | |
} else { | |
$LocalPrompt = $Prompt | |
} | |
$items = $LocalPrompt.split('#') | |
$items | & { | |
process { | |
if (-not($_)) { | |
} elseif ($_ -match '([SPLECAUMIWDVG]):?([^~ ]*)~?([^ ]*)') { | |
$splat = @{} | |
if ($matches[3]) { | |
$splat['ForegroundColor'] = $matches[3] | |
} | |
if ($matches[1] -eq 'S') { | |
$splat['Object'] = $_.Replace($matches[0], "PS") | |
} elseif ($matches[1] -eq 'E') { | |
$splat['Object'] = $_.Replace($matches[0], ">") | |
} elseif ($matches[1] -eq 'P') { | |
$splat['Object'] = $_.Replace($matches[0], (Get-Location).Path) | |
} elseif ($matches[1] -eq 'C') { | |
if ($matches[2].StartsWith('$')) { | |
$var = Get-Variable -Name $matches[2].substring(1) | |
$splat['Object'] = $_.Replace($matches[0], $var ) | |
} elseif ($matches[2].StartsWith('&') ) { | |
$var = & $(Get-Variable -Name $matches[2].substring(1)).Value | |
$splat['Object'] = $_.Replace($matches[0], $var ) | |
} else { | |
$splat['Object'] = $_.Replace($matches[0], $matches[2]) | |
} | |
} elseif ($matches[1] -eq 'A') { | |
$elevated = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) | |
if ($elevated) { | |
$splat['Object'] = $_.Replace($matches[0], '#') | |
} else { | |
$splat['Object'] = $_.Replace($matches[0], '$') | |
} | |
} elseif ($matches[1] -eq 'U') { | |
$splat['Object'] = $_.Replace($matches[0], $env:username.trim()) | |
} elseif ($matches[1] -eq 'M') { | |
$splat['Object'] = $_.Replace($matches[0], $env:COMPUTERNAME.trim()) | |
} elseif ($matches[1] -eq 'I') { | |
$splat['Object'] = $_.Replace($matches[0], $PID) | |
} elseif ($matches[1] -eq 'L') { | |
$splat['Object'] = $_.Replace($matches[0], $(Split-Path -Path (Get-Location).Path -Leaf )) | |
} elseif ($matches[1] -eq 'W') { | |
$splat['Object'] = $_.Replace($matches[0], $((Get-Process -pid $PID).WS / 1Mb)) | |
} elseif ($matches[1] -eq 'D') { | |
$splat['Object'] = $_.Replace($matches[0], $env:USERDOMAIN.Trim() ) | |
} elseif ($matches[1] -eq 'V') { | |
$splat['Object'] = $_.Replace($matches[0], $psversiontable.PSVersion.Major ) | |
} elseif ($matches[1] -eq 'G') { | |
$GitStatus = Get-GitStatus | |
$Branch = "" | |
if ($GitStatus) { | |
$Branch = " $($GitStatus.branch)" | |
if ($GitStatus.HasUntracked) { | |
$Branch += "*" | |
} | |
if ($GitStatus.AheadBy -gt 0 ) { | |
$Branch += ">$($GitStatus.AheadBy)" | |
} elseif ($GitStatus.BehindBy -gt 0) { | |
$Branch += "<$($GitStatus.BehindBy)" | |
} | |
} | |
$splat['Object'] = $_.Replace($matches[0], "$($Branch)" ) | |
} | |
Write-Host -NoNewline @splat | |
} | |
} | |
} | |
if ($Title) { | |
$items = $Title.split('#') | |
$items = $items | & { | |
process { | |
if (-not($_)) { | |
} elseif ($_ -match '([SPLECAUMIWDV]):?([^~ ]*)~?([^ ]*)') { | |
if ($matches[1] -eq 'S') { | |
$_.Replace($matches[0], "PowerShell") | |
} elseif ($matches[1] -eq 'E') { | |
$_.Replace($matches[0], ">") | |
} elseif ($matches[1] -eq 'P') { | |
$_.Replace($matches[0], (Get-Location).Path) | |
} elseif ($matches[1] -eq 'C') { | |
if ($matches[2].StartsWith('$')) { | |
$var = Get-Variable -Name $matches[2].substring(1) | |
$_.Replace($matches[0], $var ) | |
} elseif ($matches[2].StartsWith('&') ) { | |
$var = & $(Get-Variable -Name $matches[2].substring(1)).Value | |
$_.Replace($matches[0], $var ) | |
} else { | |
$_.Replace($matches[0], $matches[2]) | |
} | |
} elseif ($matches[1] -eq 'A') { | |
$elevated = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) | |
if ($elevated) { | |
$_.Replace($matches[0], '#') | |
} else { | |
$_.Replace($matches[0], '$') | |
} | |
} elseif ($matches[1] -eq 'U') { | |
$_.Replace($matches[0], $env:username.trim()) | |
} elseif ($matches[1] -eq 'M') { | |
$_.Replace($matches[0], $env:COMPUTERNAME.trim()) | |
} elseif ($matches[1] -eq 'I') { | |
$_.Replace($matches[0], $PID) | |
} elseif ($matches[1] -eq 'L') { | |
$_.Replace($matches[0], $(Split-Path -Path (Get-Location).Path -Leaf )) | |
} elseif ($matches[1] -eq 'W') { | |
$_.Replace($matches[0], $((Get-Process -pid $PID).WS / 1Mb)) | |
} elseif ($matches[1] -eq 'D') { | |
$_.replace($matches[1], $env:userdomain.trim()) | |
} elseif ($matches[1] -eq 'V') { | |
$_.Replace($matches[0], $psversiontable.PSVersion.Major ) | |
} elseif ($matches[1] -eq 'G') { | |
$GitStatus = Get-GitStatus | |
$Branch = "" | |
if ($GitStatus) { | |
$Branch = " $($GitStatus.branch)" | |
if ($GitStatus.HasUntracked) { | |
$Branch += "*" | |
} | |
if ($GitStatus.AheadBy -gt 0 ) { | |
$Branch += ">$($GitStatus.AheadBy)" | |
} elseif ($GitStatus.BehindBy -gt 0) { | |
$Branch += "<$($GitStatus.BehindBy)" | |
} | |
} | |
} | |
} | |
} | |
} | |
$host.ui.RawUI.WindowTitle = $items -join '' | |
} | |
Return " " | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment