A Better Prompt -- of Jobs and UNC Paths

Comments None

At work, I’ll often fire off a few background jobs, and then forget they’re there. If only there were an easy way to remind myself I have a job running, and to check if it is complete or not.

Okay, so I would not have led in that way if I did not have a solution to propose. Like any good shell, PowerShell is highly customizable, letting us set the command prompt to basically whatever we want. All we have to do is create a function called “prompt” and stick it in our profile script.

Here’s mine:

Function Prompt {
	$Jobs = Get-Job
	IF ($Jobs) {
		$RunningJobCount = $jobs | ?{$_.State -eq "Running"} | Measure-Object | select-object -expandproperty Count
		$OtherJobCount = $jobs | ?{$_.State -notin "Running","Completed"} | Measure-Object | select-object -expandproperty Count
		$CompletedJobCount = $jobs | ?{$_.State -eq "Completed"} | Measure-Object | select-object -expandproperty Count
		Write-host "[J $($RunningJobCount):$($OtherJobCount):$($CompletedJobCount)] " -NoNewLine
	$Loc = Get-Location
	$path = $Loc.Path
	$color = $global:host.ui.rawui.foregroundcolor
	Switch ($Loc.Provider.Name) {
		{$_ -eq "FileSystem"} {
			$Color = "Green"
			IF ($Path -match "^(Microsoft\.PowerShell\.Core\\FileSystem\:\:)") {
				$Path = $path -replace "^(Microsoft\.PowerShell\.Core\\FileSystem\:\:)"
		{$_ -eq "CMSite"} {
			$color = "Cyan"
		{$_ -eq "Registry"} {
			$color = "Red"
		{$_ -eq "ActiveDirectory"} {
			$color = "Magenta"
	Write-Host "$($path) " -nonewline -ForegroundColor $color
	Write-host ">" -NonewLine -ForegroundColor Yellow
	Write-Output " "

This provides several things. First, it looks for background jobs and displays the count before the prompt. If I issue a command like

Start-Job {Start-Sleep 5}

it will display [J 1:0:0] before the prompt. The first digit indicates the number of running jobs, the last digit indicates completed jobs, and the middle digit is for jobs that are neither running nor complete—usually, failed jobs. So every time I hit “enter”, or after running a command, the prompt refreshes and displays updated information. Once all the jobs are in the completed column, I can simply run

Remove-Job *

to clear them out, or use Receive-Job if they need to first be received. When they are all cleared, that portion of the prompt disappears.

The other thing this prompt script does is colorize the location based on PSProvider and remove the junk before UNC paths. You can see above in the Switch statement that if I am in ActiveDirectory, the path is magenta, if I am in Configuration Manager, it is cyan, if I am in the registry, it is red. It defaults to green for the file system.

To illustrate, I browsed to the UNC path of my own admin share. It takes our prompt from looking like this:

To this:

Much easier on the eyes, wouldn’t you agree? Even with the transient jobs information, it is still far shorter. Of course, there are many other kinds of information you could display. You could provide the status of BitsTransfer jobs, CPU utilization, the time of day… anything. So get creative and spruce up that shell a little more.

You’re probably reading this because you work in an MS shop like me. If that’s where we live and work, we really ought to take a few minutes to spruce up the place!



There are currently no comments on this article.


Enter your comment below. Fields marked * are required. You must preview your comment before submitting it.

← Older Newer →