Skip to content

Instantly share code, notes, and snippets.

@jdhitsolutions
Last active November 4, 2022 11:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jdhitsolutions/df808116f9234c070bdaf233418ec59b to your computer and use it in GitHub Desktop.
Save jdhitsolutions/df808116f9234c070bdaf233418ec59b to your computer and use it in GitHub Desktop.
A fancy PowerShell prompt function that should work cross-platform. See script comments for more information.
#requires -version 5.1
<#
Create a lined box with user and location information. The line color will indicate if the user is running elevated.
The prompt will also display the current date and time and a PS prompt with the PowerShell version.
┌─────────────────────────┐
│ [BOVINE320\Jeff] D:\iso │
└─────────────────────────┘
[01/18/2019 09:30:53] PS v6.1.2>
This prompt should run cross platform, although Linux testing has been limited.
To use the prompt dot source the script file in your PowerShell profile script.
This may not display properly in the Visual Studio Code PowerShell integrated console
#>
Function prompt {
if ($env:userdomain -AND $env:username) {
$me = "$($env:userdomain)\$($env:username)"
}
elseif ($env:LOGNAME) {
$me = $env:LOGNAME
}
else {
$me = "PSUser"
}
$text = "[$me] $($executionContext.SessionState.Path.CurrentLocation)"
if ($IsLinux) {
if ($(id -g) -eq 0 ) {
#running as SU
$lineColor = "Red"
}
else {
$lineColor = "Green"
}
}
elseif ($isWindows -or $psEdition -eq 'desktop') {
$IsAdmin = [System.Security.Principal.WindowsPrincipal]::new([System.Security.Principal.WindowsIdentity]::GetCurrent() ).IsInRole("Administrators")
if ($IsAdmin) {
$lineColor = "Red"
}
else {
$lineColor = "Green"
}
}
else {
#for everything else not tested
$lineColor = "Yellow"
}
$top = "┌$(("─" * $($text.length+2)))┐"
Write-Host $top -ForegroundColor $lineColor
Write-Host "│ " -ForegroundColor $lineColor -NoNewline
Write-Host $text -NoNewline
Write-Host " │" -ForegroundColor $lineColor
$bottom = "└$(("─" * $($text.length+2)))┘"
Write-Host $bottom -ForegroundColor $lineColor
#parse out the PSVersionTable to most meaningful values
$ver = $(([regex]"\d+\.\d+.\d+").match($psversiontable.psversion).value)
Write-Host "[$(Get-Date)]" -ForegroundColor Yellow -NoNewline
#the prompt function needs to write something to the pipeline
" PS v$ver$('>' * ($nestedPromptLevel + 1)) "
}
@jdhitsolutions
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment