# ----------------------------------------------------------------------------- # Script: ElapsedTimePrompt.ps1 # Author: Jeffery Hicks # http://jdhitsolutions.com/blog # Date: 10/14/2010 # Keywords: Prompt,date,location # Comments: # Don't try to run this file. Copy and paste one of the functions into your PowerShell # session or your profile. # # Learn more with a copy of Windows PowerShell 2.0: TFM (SAPIEN Press 2010) # ----------------------------------------------------------------------------- #return elapsed time the PowerShell session started 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 #return how much time to go on a daily basis 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