TThe other day I shared my simple PowerShell prompt function that displayed a few pieces of potentially useful information in a color coded box. Today I have a slight variation that also contains a few improvements. One of the things that I was torn with in my previous version was the prompt itself. I like to have as much room at the prompt as possible to write my PowerShell expression. Showing the date and version number ate up a bit too much space. So I moved the date and time inside the box as a second line.
The Adjustments
In order to get the length of the box properly calculated and to pad the text lines so that everything lines up, I ended up sorting the line lengths to get the longest one.
#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
I also used this value to pad each line with blank spaces.
Write-Host $text2.PadRight($longest, ' ') -NoNewline -ForegroundColor yellow
The Code
The complete function is on Github.
PowerShell Prompts
Here are some examples of the prompt in action.
If you try this out and modify it, I hope you'll let me know what you came up with. Have a great week.
Hello Jeffery,
This one is my favorite.
Any idea how can I change the prompt in a PS Session?
There is no $profile variable showing where the profile scripts are located.
The easiest way is to simply dot source a script file that contains the prompt function you want. You don’t automatically get profile scripts in a remote session, but as long as you can run scripts, you can dot source any script you want to use as a “profile” script. Including one with a prompt function.
Dot sourcing is not a fully automated way to have a prompt, but it works fine and it’s better than altering all users’ prompts by changing the session configuration or using a function each time you want to enter a PSSession. I’ve adopted this method. Thanks!
To add a custom prompt in a remoting session, that’s your only choice I think. Your only other option would be to setup a JEA endpoint for yourself. But that’s a whole lot more work than dot sourcing a script file!