Skip to content

Instantly share code, notes, and snippets.

@jdhitsolutions
Created January 21, 2019 15:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jdhitsolutions/6607ad1d283d92b45cb20a75da49dc24 to your computer and use it in GitHub Desktop.
Save jdhitsolutions/6607ad1d283d92b45cb20a75da49dc24 to your computer and use it in GitHub Desktop.
A variation on my PowerShell box prompt. This should work cross-platform on PowerShell Core.
#requires -version 5.1
<#
This is a variation of the BoxPrompt code you can find at https://gist.github.com/jdhitsolutions/df808116f9234c070bdaf233418ec59b
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] C:\ │
│ 1/21/2019 10:49:22 AM │
└───────────────────────┘
v5.1.17763 PS>
This prompt should run cross platform, although Linux testing has been limited.
┌─────────────────────┐
│ [jeff] /home/jeff │
│ 1/21/19 10:49:18 AM │
└─────────────────────┘
v6.1.2 PS>
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 {
#last resort
$me = "PSUser"
}
#define lines of text to include in the box
$text1 = "[$me] $($executionContext.SessionState.Path.CurrentLocation)"
$text2 = ((Get-Date).ToString()).trim()
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"
}
#get the length of the longest line in the box and uas that to calculate lengths and padding
$longest = $text1.length, $text2.length | Sort-Object | Select-Object -last 1
$len = $longest + 2
$top = "┌$(("─" * $len))┐"
Write-Host $top -ForegroundColor $lineColor
Write-Host "│ " -ForegroundColor $lineColor -NoNewline
Write-Host $text1.PadRight($longest, ' ') -NoNewline
Write-Host " │" -ForegroundColor $lineColor
Write-Host "│ " -ForegroundColor $lineColor -NoNewline
Write-Host $text2.PadRight($longest, ' ') -NoNewline -ForegroundColor yellow
Write-Host " │" -ForegroundColor $lineColor
$bottom = "└$(("─" * $len))┘"
Write-Host $bottom -ForegroundColor $lineColor
#parse out the PSVersionTable to most meaningful values
$ver = $(([regex]"\d+\.\d+.\d+").match($psversiontable.psversion).value)
#the prompt function needs to write something to the pipeline
" v$($ver) PS$('>' * ($nestedPromptLevel + 1)) "
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment