During the course of writing a few scripts that refresh a specific part of the console, such as the recent Read-Host alternative, I realized that flashing colors wasn't always necessary. The fact that I could update the same space on the screen meant I could write the same content with minor changes and it would look like the the screen as "flipping". Essentially I was thinking of a clock.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
So I thought it might be handy to have a clock as part of my PowerShell prompt. PowerShell has a built-in function called Prompt but you can replace it with your own version. The function will only last for as long as your PowerShell session so if you don't like it, exit and restart PowerShell.
Function Prompt { #get current cursor position $Coordinate = $host.ui.RawUI.CursorPosition $running = $True #set a variable to determine if the user is typing something $Typing = $False While ($Running) { #set the cursor position $host.ui.rawui.CursorPosition=$Coordinate $p = "[{0}] PS {1}>" -f (Get-Date -DisplayHint time),(Get-Location).Path Write-Host $p -NoNewline Start-Sleep -milliseconds 250 if (-Not $Typing) { #see if a key has been pushed if ($host.ui.RawUi.KeyAvailable) { #user is typing $Typing = $True $running = $False #function needs to return something return " " } #if key available } #if not typing } #while } #end function
This is for the most part the basic function that shows PS and your current location. This prompt function will not work properly in the PowerShell ISE. The magic happens by always setting the cursor to the same coordinates in the PowerShell shell console. I use the same type of While loop I used in my other functions, only this time I'm waiting for the user to press any key, which would indicate the start of typing a command. Once that has been detected, the looping stops and the time ceases to be refreshed in the prompt.
You really need to see this live but here's a screenshot example.
Then I thought it might be helpful to have the clock stand out so I added a little color.
Function Prompt { <# this version writes the clock in a different color #> #get current cursor position $Coordinate = $host.ui.RawUI.CursorPosition #variable for looping $running = $True #set a variable to determine if the user is typing something $Typing = $False While ($Running) { #set the cursor position $host.ui.rawui.CursorPosition=$Coordinate $d = "[{0}]" -f (Get-Date -DisplayHint time) $p = " PS {0}>" -f (Get-Location).Path Write-Host $d -NoNewline -ForegroundColor Yellow Write-Host $p -NoNewline Start-Sleep -milliseconds 250 if (-Not $Typing) { #see if a key has been pushed if ($host.ui.RawUi.KeyAvailable) { #user is typing $Typing = $True $running = $False #function needs to return something return " " } #if key available } #if not typing } #while } #end function
The only issue I've found with these prompts, is that if you need to scroll in the console window, you'll need to press the spacebar or type something so that the clock stops refreshing. Otherwise you are scrolling while PowerShell is trying to write to the console.
Instead of clock you could use a countdown timer. Or perhaps some sort of performance counter. For a prompt though, you need to make sure you can get and display the information in a few hundred milliseconds, otherwise the prompt will feel sluggish and unresponsive.
Enjoy and let me know where this leads you.
Hi Jeff,
nice code, thanks. With start-transcript command the output is not so nice 🙂
Example:
**********************
Windows PowerShell transcript start
Start time: 20140827081106
Username : domain\user
Machine : WS-01 (Microsoft Windows NT 6.3.9600.0)
**********************
Transcript started, output file is c:\transcripts\transcript.txt
[27.8.2014 8:11:06] PS C:\>[27.8.2014 8:11:06] PS C:\>[27.8.2014 8:11:06] PS C:\>[27.8.2014 8:11:07] PS C:\>[27.8.2014 8:11:07] PS C:\>[27.8.2014 8:11:07] PS C:\>[27.8.2014 8:11:08] PS C:\>[27.8.2014 8:….. And so on.
I use start-transcript cmdlet with my customized shell.
Yeah, probably not. But I would run this in a separate PowerShell window or ISE tab. Or even better, use the -AsJob parameter. Then the transcript is irrelevant to this.
Awesome trick… Thanks Jeff… I learnt a lot from your blogs…