Skip to content

Instantly share code, notes, and snippets.

@JohnL4
Last active November 12, 2018 22:13
Show Gist options
  • Save JohnL4/bd665a53fd143f47d48f774d68318cdd to your computer and use it in GitHub Desktop.
Save JohnL4/bd665a53fd143f47d48f774d68318cdd to your computer and use it in GitHub Desktop.
Quick-n-dirty reverse-video bash and PowerShell prompts
# Update the following two variables to modify the window title more-or-less permanently. Otherwise, your attempt to
# update the window title will be immediately wiped out by this prompt function.
# Update these variables from other functions like this: Set-Variable -Name PROMPT_TITLE_PREFIX -Value $title -Scope Global
$PROMPT_TITLE_PREFIX = ""
$PROMPT_TITLE_SUFFIX = ""
<#
.SYNOPSIS
Returns true iff current PowerShell session is running with elevated privileges (i.e., "As Administrator").
.NOTES
From http://ss64.com/ps/syntax-elevate.html
#>
function IsAdmin
{
([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
}
function New-WindowTitle {
$userLocn = ("{0}@{1}" -f $env:USERNAME, $(get-location))
return ("{0} {1} {2}" -f $PROMPT_TITLE_PREFIX, $userLocn, $PROMPT_TITLE_SUFFIX)
}
function prompt
{
# Note that DarkMagenta is the color slot used in the default PowerShell Start Menu shortcut for the console
# window's background color. Note also that, as of PS 2.0, the online help for Write-Host lists the colors in
# color-slot order (if that makes any sense).
if (IsAdmin)
{
$Host.UI.RawUI.WindowTitle = New-WindowTitle
Write-Host ("PS " + $(get-location)) -nonewline -backgroundcolor gray -foregroundcolor DarkMagenta # -foregroundcolor Green
Write-Host "#" -nonewline -backgroundcolor Gray -foregroundcolor DarkRed
return " "
}
else
{
$Host.UI.RawUI.WindowTitle = New-WindowTitle
Write-Host ("PS " + $(get-location) +">") -nonewline -backgroundcolor gray -foregroundcolor DarkMagenta # -foregroundcolor Green
return " "
}
}
export PS1='\[\e[7m\]\h#\[\e[0m\] '
# Single quotes are important, otherwise you have to escape all your backslashes.
# \[ -- start non-printing characters
# \] -- end non-printing characters (duh?)
# \e -- ASCII ESC character
# \h -- current host name
#
# I used "#" because I'm running as root; otherwise you should probably use "$".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment