Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Friday Fun with a Cross-Platform PowerShell Prompt

Posted on January 18, 2019January 18, 2019

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.

Manage and Report Active Directory, Exchange and Microsoft 365 with
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.

Windows PowerShell elevated

Windows PowerShell non-elevated

PowerShell Core on Linux

PowerShell Core on Windows elevated

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.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

1 thought on “Friday Fun with a Cross-Platform PowerShell Prompt”

  1. Pingback: Thinking Outside the Box with Another PowerShell Prompt • The Lonely Administrator

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d