Categories

Fun with prompts

At the PowerShell Jumpstart event there were a lot questions asking how Jeffrey Snover managed to get a dollar sign – $ – as his prompt. A lot of them centred on asking if this was a PowerShell v4 thing.

Its not a V4 thing. You have been able to change the prompt since v1. 

The PowerShell prompt is by default the path of your current folder. This can easily take half the width if the screen if if you are several layers deep

You can change the PowerShell prompt by running a prompt function. For instance to change the prompt to a dollar sign

PS C:\Windows\system32> function prompt {
>> "$ "
>> }
>>
$

I move the folder information into the header

$ function prompt {
>> "$ "
>> $host.ui.RawUI.WindowTitle = $(get-location)
>> }
>>

Other useful tricks – are you running 32 bit or 64 bit PowerShell?  Its easy to forget if you have numerous consoles open.

PS>function prompt {
>> if ([System.IntPtr]::Size -eq 8) {$size = '64 bit'}
>> else {$size = '32 bit'}
>> $host.ui.RawUI.WindowTitle = "$size $(get-location)"
>> "$ "
>> }
>>
$

This adds 64 bit or 32 bit to the console header.  As well as the location.

One final option is to show if you are running that console elevated or not.

function prompt {
if ([System.IntPtr]::Size -eq 8) {$size = '64 bit'}
else {$size = '32 bit'}

$currentUser = [Security.Principal.WindowsIdentity]::GetCurrent()
$secprin = New-Object Security.Principal.WindowsPrincipal $currentUser
if ($secprin.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator))
{$admin = 'Administrator'}
else {$admin = 'non-Administrator'}

$host.ui.RawUI.WindowTitle = "$admin $size $(get-location)"
"$ "
}

The Windows identity of the current user is retrieved and used to create a security principal object. This is used to test is the security principal is in the Administrator role.

Add this prompt function to your profile and you get some useful reminders for while you are working and a minimalist prompt.

Leave a Reply