Tags


I’ve seen various posts and scripts about customising your PowerShell prompt.  From these, I have over time built up my own Prompt function.  I’m sorry I can’t recall from whom the different constituent bits came originally.  One person pointed out that a long path in the prompt can get annoying and I’m inclined to agree.  I’ve therefore come up with this.  My own idea was to add a feature to indicate just the current PSProvider in the prompt itself and move the path to the title bar instead.  There it sits along with the bitness, admin-status and also the PowerShell version which I added.  Here’s a screenshot:

PSPrompt

And here’s the sourcecode from my Microsoft.PowerShell_profile.ps1:

function prompt {
if ([System.IntPtr]::Size -eq 8) {
$bitness = '64 bit'
} else {
$bitness = '32 bit'
}
$currentWindowsIdentity = [Security.Principal.WindowsIdentity]::GetCurrent()
$currentWindowsPrincipal = New-Object -TypeName System.Security.Principal.WindowsPrincipal -ArgumentList $currentWindowsIdentity
$adminRole = [Security.Principal.WindowsBuiltInRole]::Administrator
if ( $currentWindowsPrincipal.IsInRole($adminRole) ) {
$admin = 'Administrator'
} else {
$admin = 'non-Administrator'
}
$path = $executionContext.SessionState.Path.CurrentLocation.Path
$provider = $executionContext.SessionState.Drive.Current.Provider.Name
$host.ui.RawUI.WindowTitle = "$($admin): Windows PowerShell v$($host.version.ToString()) ($bitness) - [$path]"
Write-Output "PS $provider`:$('>' * ($nestedPromptLevel + 1)) "
}

I know it will take some getting used to having the path in the title bar but I think it will be worth getting used to!

Advertisements