Customising Your PowerShell Prompt

Posted on Posted in PowerShell

It’s been an age since I’ve written anything and I have a massive amount of posts which I still want to get out.

Like with any Windows admin you’ll be working with PowerShell on a daily basis, so why not tweak your “prompt” so it suits you. To modify your prompt firstly you’ll need to understand how PowerShell Profiles work, so have a quick gander over here before you start;

Windows PowerShell Profiles
http://msdn.microsoft.com/en-us/library/windows/desktop/bb613488(v=vs.85).aspx

For my prompt I like to inject a little bit of pizazz and extra information, after all a little bit of information goes a long way as they say. This prompt is pretty much based on my many years of using Linux and have a custom BASH prompt.

I like to see the current time, who I’m logged in as on what machine (ideal of PSRemoting) and last of all, what directory I am in and whether I’m an Administrator on a given machine or not.

The following code;


function prompt
{

# Set Window Title
$host.UI.RawUI.WindowTitle = "$ENV:USERNAME@$ENV:COMPUTERNAME - $(Get-Location)"

# Set Prompt
Write-Host (Get-Date -Format G) -NoNewline -ForegroundColor Red
Write-Host " :: " -NoNewline -ForegroundColor DarkGray
Write-Host "$ENV:USERNAME@$ENV:COMPUTERNAME" -NoNewline -ForegroundColor Yellow
Write-Host " :: " -NoNewline -ForegroundColor DarkGray
Write-Host $(get-location) -ForegroundColor Green

# Check for Administrator elevation
$wid=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$prp=new-object System.Security.Principal.WindowsPrincipal($wid)
$adm=[System.Security.Principal.WindowsBuiltInRole]::Administrator
$IsAdmin=$prp.IsInRole($adm)
if ($IsAdmin) {
Write-Host "(admin) #" -NoNewline -ForegroundColor Gray
return " "
}
else {
Write-Host ">" -NoNewline -ForegroundColor Gray
return " "
}
}

Should give you something like this. Notice the first PowerShell prompt has no “(admin) #” as you’ve not elevated yourself with Administrator privileges, as opposed to the second one. A neat little UAC admin indicator.

Non Admin

Admin

One powerful feature is leveraging the jobs infrastructure. If you have any long running scripts you can set it to runasjob with the Invoke-Command. The prompt will show you any jobs with a Running state and then disappear once they have finished. Here’s the altered jobs version of the above prompt for PowerShell v3


function prompt
{

# Set Window Title
$host.UI.RawUI.WindowTitle = "$ENV:USERNAME@$ENV:COMPUTERNAME - $(Get-Location)"

# Set Prompt
Write-Host (Get-Date -Format G) -NoNewline -ForegroundColor Red
Write-Host " :: " -NoNewline -ForegroundColor DarkGray
Write-Host "$ENV:USERNAME@$ENV:COMPUTERNAME" -NoNewline -ForegroundColor Yellow
Write-Host " :: " -NoNewline -ForegroundColor DarkGray
Write-Host $(get-location) -ForegroundColor Green

# Check Running Jobs
$jobs = Get-Job -State Running
$jobsCount = $jobs.Count

# Check for Administrator elevation
$wid=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$prp=new-object System.Security.Principal.WindowsPrincipal($wid)
$adm=[System.Security.Principal.WindowsBuiltInRole]::Administrator
$IsAdmin=$prp.IsInRole($adm)
if ($IsAdmin) {
if ($jobsCount -eq $null) {
Write-Host "(admin) #" -NoNewline -ForegroundColor Gray
return " "
}
else {
Write-Host "(admin) jobs:" $jobsCount -NoNewline -ForegroundColor Gray
Write-Host "#" -NoNewline -ForegroundColor Gray
return " "
}
}
else {
if ($jobsCount -eq $null) {
Write-Host ">" -NoNewline -ForegroundColor Gray
return " "
}
else {
Write-Host "jobs:" $jobsCount -NoNewline -ForegroundColor Gray
Write-Host ">" -NoNewline -ForegroundColor Gray
return " "
}
}
}

Enjoy.

Kristof

One thought on “Customising Your PowerShell Prompt

Leave a Reply

Your email address will not be published. Required fields are marked *