≡ Menu

PsTip# Add date and time to Powershell Prompt

Sometimes, we may want to know when a particular Powershell command was executed in our current shell. Simply scrolling through the powershell history will not help you as it just shows the commands. I thought having current date and time as part of poweshell prompt improves the situation to a greater level. It also helps you in recording your changes in proper way.

Here is a small piece of code which helps you to add current date time to powershell prompt. You can change the formatting of the date and time to meet your requirements. But make sure to keep the function name same

Code:

function prompt            
{            
    "PS " + $(get-location) + " [$(Get-Date)]> "            
}

Usage and Output:

You can clearly see in the above picture that, I simply copy pasted the code into powershell prompt and it started showing the date time stamp in the prompt section. The date and time are changing when switched to a new directory or simply pressed enter or when entering a command.

This is very helpful for tracking. Add this to your powershell profile to get this prompt every time you open powershell window.

Comments on this entry are closed.

  • al May 27, 2016, 6:15 pm

    great tip ! Can you do the same on the command line ? (cmd)
    thanks

    • Wintel Rocks June 19, 2016, 9:31 pm

      Hi, haven’t tried with command prompt before.

    • Michael March 26, 2017, 8:25 am

      prompt CMD $p [$d $t]$g

  • js2010 April 27, 2018, 1:22 am

    No need for the +’s:

    function prompt { “PS $(get-location) [$(Get-Date)]> ” }

    • Wintel Rocks May 29, 2018, 6:11 pm

      Thanks for the feedback. It works as well.

  • Chris February 21, 2019, 11:09 pm

    Maybe I’m being greedy here, buy is there a way to make that permanent so you don’t have to re-enter the command every time you open Power Shell?