This year is turning out to be all things cross-platform for me. Continuing this line of discussion I have something fun and simple today. A PowerShell prompt function that will work cross-platform and provide some meaningful information in what I think is a elegant manner. You may not need the function, but you might want to see how I create a PowerShell function that takes cross-platform compatibility into mind.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
The Goal
The design goal was to create a prompt that would display the user name and current location in a colored box. The box line color would indicate if the user was running in an elevated PowerShell session. The prompt would then display the current date and time and the PowerShell version. But you should really think of the function as a proof-of-concept or a template. You could substitute in any information that is relevant to you. Although, you need to keep cross-platform compatibility in mind.
Developing a Cross-Platform Function
I knew that I wanted to show the current user name as part of the prompt. There are several techniques you could use. I decided to test if $env:Username is defined. This variable is NOT defined in non-Windows platforms. On Linux, there should be a LOGNAME environmental variable. If for some reason I can't find any of these values, then I'll use a last resort default.
if ($env:userdomain -AND $env:username) { $me = "$($env:userdomain)\$($env:username)" } elseif ($env:LOGNAME) { $me = $env:LOGNAME } else { $me = "PSUser" }
I should point out that my testing on Linux has been limited and I have not tested anything on a Mac since I don't have access to one. I use this value in the text I intend to display inside a box.
$text = "[$me] $($executionContext.SessionState.Path.CurrentLocation)"
You could add additional information in this text string. I'm adding the current location. You could add the hostname, an IP address, free space or whatever you need. Keep in mind you should write code to derive the values according to the version of PowerShell and platform.
For example, if the user (me) is running in an elevated PowerShell session, I want the box color to be Red. Otherwise it should be Green. On Windows platforms I can use the .NET class System.Security.Principal.WindowsPrincipal. However, that class is not available in PowerShell Core on non-Windows platforms. I used an If/ElseIf/Else construct to test some new variables in PowerShell Core or Windows PowerShell.
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" }
Notice that I have a an Else clause to handle all other situations. Depending on your project, you might want to throw an exception. But since this is a prompt function I don't want to do that.
The PowerShell Prompt Function
You can find the complete function as a gist on GitHub.
Like other PowerShell prompt functions you need to dot source the script.
. C:\scripts\boxprompt.ps1
If you want to use this all the time, put this line in your PowerShell profile script.
Here's a sample of the prompt in a variety of PowerShell sessions.
The end result is that I have a simple PowerShell prompt function that provides a bit more information that runs on any PowerShell instance. And there's room to grow because I can add other information to the text string in the box if necessary.
I hope you'll kick this around and let me know what you think. Have a great weekend.
1 thought on “Friday Fun with a Cross-Platform PowerShell Prompt”
Comments are closed.