Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

A Timely PowerShell Prompt

Posted on August 20, 2014

021913_2047_WordTest1.pngDuring 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.

Manage and Report Active Directory, Exchange and Microsoft 365 with
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.
time-prompt-1

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

time-prompt-2

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.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

3 thoughts on “A Timely PowerShell Prompt”

  1. Lasse says:
    August 27, 2014 at 4:12 am

    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.

    1. Jeffery Hicks says:
      August 27, 2014 at 7:20 am

      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.

  2. Arun Kumar Anala says:
    September 22, 2014 at 2:11 am

    Awesome trick… Thanks Jeff… I learnt a lot from your blogs…

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d