I haven't written a Friday Fun post in quite a while. Often these posts don't have much practical value but hopefully illustrate a concept or technique. Although what I have today is something you could use immediately. I have a version of a PowerShell prompt function that will color code your location based on the underlying PSProvider. The function will work on Windows PowerShell and appears to work just fine on PowerShell Core running under Windows. But PowerShell Core on Linux appears to process the prompt function a bit differently so I don't recommend using it. Here's how it all works.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
PowerShell has a built in function called prompt. But you can redefine this function just about any way you'd like. I've shared a number of prompt functions over the year. Here's what I'm trying out today.
The prompt will change the color of your location. I'm also inserting the current time. Here's the code I am using.
#this won't display properly on Linux Function prompt { # .Description # This custom version of the PowerShell prompt will present a colorized location value based on the current provider. It will also display the PS prefix in red if the current user is running as administrator. # .Link # https://go.microsoft.com/fwlink/?LinkID=225750 # .ExternalHelp System.Management.Automation.dll-help.xml $user = [Security.Principal.WindowsIdentity]::GetCurrent() if ( (New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)) { $adminfg = "Red" } else { $adminfg = $host.ui.rawui.ForegroundColor } Switch ((get-location).provider.name) { "FileSystem" { $fg = "green"} "Registry" { $fg = "magenta"} "wsman" { $fg = "cyan"} "Environment" { $fg = "yellow"} "Certificate" { $fg = "darkcyan"} "Function" { $fg = "gray"} "alias" { $fg = "darkgray"} "variable" { $fg = "darkgreen"} Default { $fg = $host.ui.rawui.ForegroundColor} } Write-Host "[$((Get-Date).timeofday.tostring().substring(0,8))] " -NoNewline Write-Host "PS " -nonewline -ForegroundColor $adminfg Write-Host "$($executionContext.SessionState.Path.CurrentLocation)" -foregroundcolor $fg -nonewline Write-Output "$('>' * ($nestedPromptLevel + 1)) " }
The main part is a Switch statement that assigns a color variable based on the underlying PS Provider for the current location. I then use Write-Host to build the prompt, displaying different parts in different colors. The prompt function needs to return something so the last line uses Write-Output to provide the >. You may also have noticed some code at the beginning of the function. If the current user is running in an elevated session, then I want to display the PS portion of the prompt in red to provide a visual reminder.
To use this prompt, I can put the function in a profile script, or I can put it in a separate file and dot source it at any time.
I hope you'll give it a spin to at least experience it. The worst case, restart your PowerShell session and you'll be back to normal. Have a great weekend.
Very cool. Thank you for sharing.
Hey Jeff – hope all well – have a good PSSummit 🙂
Replace:
“[$((Get-Date).timeofday.tostring().substring(0,8))] ”
With:
“[{0:HH:mm:ss}] ” -f (Get-Date)
(or, if you don’t want to use the -f operator and keep it all in one string for Write-Host)
“[$(Get-Date -Format ‘HH:mm:ss’)] “
This PowerShell prompt function is fantastically helpful to anyone wanting to keep track of their location by coloring their code. I am always interested in ways to keep track of my lines of code, because after hours of work on it, my eyes tend to cross! Hopefully this will help to boost my IT productivity, by cutting time down that’s wasted on searching through lines of code to find what I need. Thank you for providing such detailed code!
I like to include the history ID in the prompt so I can later easly trigger PSreadlines #ID functionality to repeat a command.
(Get-History).count + 1