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.
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.
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)) "; }
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)) "; }
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)) "; }
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)) "; }
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)) "; }
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)) " }
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.
Hi great article, but the photos don’t match the code examples at all.
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?
Thank you
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!!!