Skip to content

Instantly share code, notes, and snippets.

@Vivelin
Last active May 16, 2019 07:33
Show Gist options
  • Save Vivelin/e203e992a506d613d46ef4db7d92542c to your computer and use it in GitHub Desktop.
Save Vivelin/e203e992a506d613d46ef4db7d92542c to your computer and use it in GitHub Desktop.
Basic Powerline-inspired PowerShell prompt

Save the profile.ps1 file to $PROFILE.AllUsersAllHosts as administrator.

# Note: requires a Powerline-patched font to display properly
# See https://github.com/powerline/fonts
$CURRENT_BG = $null
$SEGMENT_DELIMETER_GLYPH = ""
$GIT_BRANCH_GLYPH = ""
function prompt {
Write-PowerlineUserContext
Write-PowerlineLocation
Write-PowerlineGit
Write-PowerlineSegmentEnd
# Required to prevent the default "PS>" since we use Write-Host to get colors
return " "
}
Function Write-PowerlineSegment($bg = 'Black', $fg = 'Blue', $str) {
if ($CURRENT_BG -and $CURRENT_BG -ne $bg) {
Write-Host "$SEGMENT_DELIMETER_GLYPH" -ForegroundColor $CURRENT_BG -BackgroundColor $bg -NoNewline
}
$Global:CURRENT_BG = $bg
Write-Host " $str " -ForegroundColor $fg -BackgroundColor $bg -NoNewline
}
Function Write-PowerlineSegmentEnd() {
Write-Host "$SEGMENT_DELIMETER_GLYPH" -ForegroundColor $CURRENT_BG -BackgroundColor Black -NoNewline
$Global:CURRENT_BG = $null
}
Function Write-PowerlineUserContext() {
if (Test-Administrator) {
Write-PowerlineSegment -bg Red -fg Black "${env:UserName}"
}
else {
Write-PowerlineSegment -bg Green -fg Black "${env:UserName}"
}
}
Function Write-PowerlineLocation() {
Write-PowerlineSegment -bg Blue -fg Black -str "$pwd"
}
Function Write-PowerlineGit() {
if (Get-Command git -ErrorAction SilentlyContinue) {
if ($(git rev-parse --is-inside-work-tree 2>$null)) {
$branch = $(git symbolic-ref HEAD 2>$null).ToString().Substring(11)
Write-PowerlineSegment -bg Yellow -fg Black -str "$GIT_BRANCH_GLYPH $branch"
}
}
}
Function Test-Administrator {
$user = [Security.Principal.WindowsIdentity]::GetCurrent();
(New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
Function Initialize-Host {
$bg = $Host.UI.RawUI.BackgroundColor
$Host.PrivateData.ErrorBackgroundColor = $bg
$Host.PrivateData.WarningBackgroundColor = $bg
$Host.PrivateData.DebugBackgroundColor = $bg
$Host.PrivateData.VerboseBackgroundColor = $bg
$Host.UI.RawUI.WindowTitle = "Windows PowerShell v$($PSVersionTable.PSVersion.Major).$($PSVersionTable.PSVersion.Minor)"
}
Initialize-Host
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment