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

Friday Fun with Timely PowerShell Prompts

Posted on November 30, 2018

If PowerShell is a part of your daily routine, you most likely have a console window open all day. In addition to using PowerShell to get stuff done, you can use PowerShell to keep you on track. I've written before and talked about how I use PowerShell to manage my day. You may not need to go to the lengths I take it. But you might find it worth your time to make a few minor tweaks that bring a little extra value to your PowerShell console one Enter key at a time.

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!

I'm talking of course about the Prompt function. You may not be aware but every time your press Enter, PowerShell is invoking a function called prompt that by defaults shows you the PS prefix and your current location. PowerShell defines one for you automatically.

PS C:\> Get-Command prompt

CommandType     Name                                               Version    Source                                 
-----------     ----                                               -------    ------                                 
Function        prompt

Today I'm going to show you some different variations of the prompt function that will display day and time information.  Personally, since I'm looking at my PowerShell console frequently, it helps to be able to check the time. You can try these functions in your PowerShell console, or event the PowerShell ISE and VS Code as much as you want. Unless you add the prompt function to a PowerShell profile script, you'll get the default prompt the next time your start PowerShell.

Here's a simple one that displays the current date and time.

function prompt {
"[$(Get-Date)] PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) ";
}

To try this, simply copy the function and paste it into your PowerShell session. Press Enter and you should see the new prompt.

image

I like enclosing it in brackets to set it off but you can do whatever you want.

function prompt {
"<*$(Get-Date)*> PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) ";
}

Or perhaps you'd like just the time.

function prompt {
"[$((Get-Date).toShortTimeString())] PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) ";
}

image

You might prefer more time detail, in which case use the ToLongTimeString() method instead. Or take a bit more control and format the time the way you want it.

function prompt {
"[$(Get-Date -format 'hh:mm:ss tt')] PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) ";
}

image

The format string is case-sensitive. If you prefer a 24 hour format you could use this prompt.

function prompt {
"[$(Get-Date -format 'HH:mm:ss')] PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) ";
}

image

Personally, this level of detail is sufficient. But if you need even more granularity you can add in milliseconds.

function prompt {
"[$(Get-Date -format 'HH:mm:ss:ffff')] PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) ";
}

image

I'm displaying to 4 places but you can go up to 6.

And I suppose I should show those of you with large monitors running an oversize version of the PowerShell console you can display the default Get-Date output.

function prompt {
"[$((Get-Date | Out-String).trim())] PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) ";
}

image

Before I let you go, here's one more example that gives you a taste of what you can do.

function prompt {
write-host "[$(Get-Date -f 'hh:mm:ss tt')]" -ForegroundColor Yellow -NoNewline
" PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "
}

image

I hope you are curious enough to try some of these variations. You can read more about the prompt function and how to customize it at https://go.microsoft.com/fwlink/?LinkID=225750. Have a great weekend.


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

5 thoughts on “Friday Fun with Timely PowerShell Prompts”

  1. Christian says:
    December 1, 2018 at 4:45 am

    Hi great article, but the photos don’t match the code examples at all.

    1. Jeffery Hicks says:
      December 1, 2018 at 4:17 pm

      I must be missing something because everything looks fine to me. I wonder if there is some weird rendering issue. What browser are you using on what type of device?

  2. Ahmed says:
    December 1, 2018 at 8:10 pm

    Thank you

  3. Pingback: ICYMI: PowerShell Week of 07-December-2018 | PowerShell.org
  4. Troy Heiss says:
    January 2, 2019 at 5:10 pm

    This is incredibly handy…I would output a transcript with the date, but having the time in the prompt to match up when I ran certain cmdlets is just awesome. Thank you for sharing!!!

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