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.
great tip ! Can you do the same on the command line ? (cmd)
thanks
Hi, haven’t tried with command prompt before.
prompt CMD $p [$d $t]$g
Thanks for sharing Michael.
No need for the +’s:
function prompt { “PS $(get-location) [$(Get-Date)]> ” }
Thanks for the feedback. It works as well.
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?
Hi, You can add it to powershell profiles so that it gets imported every time you start powershell.
PowerShell profiles : https://blogs.technet.microsoft.com/askpfeplat/2018/06/25/powershell-profiles-processing-illustrated/