PowerShell for Windows Admins

Jul 21 2013   9:48AM GMT

Fun with prompts



Posted by: Richard Siddaway
Tags:
PowerShell

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.

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: