Not too long ago I offered up a tasting of PowerShell prompts 3 ways. My first offering were variations on displaying the current date and time. But a PowerShell prompt can do much more. For today's Friday Fun I present a duo of of calculating prompts.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
The first item on the menu is a countdown prompt. The original intent was to offer a constant reminder of how much time was left in the work day.
[cc lang="PowerShell"]
Function Prompt {
[datetime]$End="5:00PM"
$ts=$End - (Get-Date)
#increase $end by one day if more than
#1 hour has elapsed past the deadline
While ($ts.TotalHours -le -1)
{
$End=$End.AddDays(1)
$ts=$End - (Get-Date)
}
#select a color based on how many hours are left
If ($ts.TotalHours -ge 5)
{
$fgcolor="Green"
}
elseif ($ts.TotalHours -ge 2 -AND $ts.TotalHours -lt 5)
{
$fgColor="Yellow"
}
else
{
$fgColor="Red"
}
#strip off the millisecond part with Substring(). The
#millisecond part will come after the last period
$s=$ts.ToString()
$ToGo=$s.Substring(0,$s.LastIndexOf("."))
$loc="PS {0}" -f (get-location)
Write-Host "[$ToGo]" -NoNewline -ForegroundColor $fgcolor -BackgroundColor Black
write-host $loc -NoNewline
#to use Write-Host, the function has to write something to the pipeline
Write ">"
} #end function
[/cc]
The function has a hardcoded datetime variable which represents my countdown target.
[cc lang="PowerShell"]
[datetime]$End="5:00PM"
[/cc]
Because I didn't specify a date component, $End will be 5:00PM for today. The prompt calculates a time span the current date and time to the ending date and time.
[cc lang="PowerShell"]
$ts=$End - (Get-Date)
[/cc]
The "trick" in the function is how I handle the situation when 5:00 has come and gone. The timespan will be negative, (which I take advantage of as you'll see in a moment.) My solution was that if an hour or more had passed since the cutoff date, to roll $End over to tomorrow.
[cc lang="PowerShell"]
While ($ts.TotalHours -le -1)
{
$End=$End.AddDays(1)
$ts=$End - (Get-Date)
}
[/cc]
The assumption here is that you are using the same value on a daily basis. The function uses the timespan to help determine what color to use for display. This provides helpful visual feedback about the approaching end of the day and when it is time to go home.
[cc lang="PowerShell"]
If ($ts.TotalHours -ge 5)
{
$fgcolor="Green"
}
elseif ($ts.TotalHours -ge 2 -AND $ts.TotalHours -lt 5)
{
$fgColor="Yellow"
}
else
{
$fgColor="Red"
}
[/cc]
The rest of the function, which displays the prompt, is essentially the same. The result is something like this.
The other prompt on the tasting menu is one that I find most helpful personally. Instead of counting down, the prompt "counts up", and tells me how long the current PowerShell session has been running.
[cc lang="PowerShell"]
Function Prompt {
#get start time for the current PowerShell session
#$pid is a special variable for the current PowerShell process ID
[datetime]$psStart=(get-Process -id $pid).StartTime
$ts=(Get-Date) - $psStart
#strip off the millisecond part with Substring(). The
#millisecond part will come after the last period
$s=$ts.ToString()
$elapsed=$s.Substring(0,$s.LastIndexOf("."))
$loc="PS {0}" -f (get-location)
Write-Host "[$elapsed]" -NoNewline -ForegroundColor Cyan
write-host $loc -NoNewline
#to use Write-Host, the function has to write something to the pipeline
Write ">"
} #end function
[/cc]
Every PowerShell session has a system variable called $pid, which represents the process ID of the current PowerShell session. With this piece of information, I can get the PowerShell process and its StartTime property.
[cc lang="PowerShell"]
[datetime]$psStart=(get-Process -id $pid).StartTime
[/cc]
The rest is pro-forma: calculate a time span, strip off the milliseconds and display the prompt.
As you can tell from the screen shot, this PowerShell session has been open and running for more than 3 days. Every time I press enter, a new value is calculated and displayed.
There's almost no limit to what you can include in your PowerShell prompt. I'd love to hear any special ways you're using it. In the meantime, you can download a text file with today's functions here.
Hi Jeff,
The 2 elements that I really love in my custom prompt are:
1 . the name of the Workstation/Server I’m currently running on
2. double command line,
line 1 displays my history number + computername + currentpath
Line 2 is reserved for my commands which gives me a full line to type on..
this is it
function prompt
{
# v.1.0
$cdelim = [ConsoleColor]::DarkCyan
$chost = [ConsoleColor]::Green
$cloc = [ConsoleColor]::Cyan
$num=(Get-History -count 1).id+1
$MyHost=([net.dns]::GetHostName())
write-host “[$num] e$([char]0x0A7) ” -n -f $cloc
write-host ([net.dns]::GetHostName()) -n -f $chost
write-host ‘ {‘ -n -f $cdelim
Write-Host (Get-Location).path -n -f $cloc
Write-Host ‘} ‘ -n -f $cdelim
” ” + [System.Environment]::NewLine + “>”.PadLeft((get-location -stack).Count + 1, “+”)
}
I put up a screenshot here
http://www.capri-technology.be/index.php?option=com_content&view=article&id=88%3Acustom-powershell-prompt&Itemid=61&lang=en
I have a few prompts like this. Maybe I’ll dig one or two out. Thanks for sharing.