Adding Date & Time to PowerShell's Console
Think about the amount of times you ran a PowerShell command or a script, but you didn’t remember the time it was initiated.
Sometimes the exact minute or even exact second are very important for an IT guy, to evaluate the amount of time a process was taken.
In this blog I will review a simple commands which will add the time and date to the PowerShell’s console, temporarily or permanently.
The command
There is a simple command which used to add the date and time (including the current path/location) to your PowerShell’s console:
function prompt {“PS: $(Get-Location) $(get-date)>”}
After running this command, the current PowerShell console will include the date, time and location like the next example:
Remember that after closing this console, the function will be removed and the console will include the path/location as before:
Let’s make this function permanent!
To make this change permanent, so each time the console runs it will include the date, time and path, we need to add this function to the user’s PowerShell profile.
- First of all, to locate your current user’s profile, we have to run the next command:
$PROFILE
$PROFILE is a variable which stores the path to the “Current User, Current Host” profile.
- By default, since the user’s profile is empty, its actually doesn’t exists as a file:
- To create a new profile, just run the next command:
New-Item -ItemType File $profile -Force
- Running $profile again will show us that the file was created:
- Now lets edit the $Profile file by running the next command (ii is the alias for Invoke-Item):
ii $Profile
- Type your function and save file
function prompt {“PS: $(Get-Location) $(get-date)>”}
Of course you can add more functions, aliases and more, according to your needs.
- After saving your profile, each time the PowerShell console will be opened, the date, time and location will be available for you.
Possible issues
In case your organization implemented a restriction policy for PowerShell, you will get the next error, while running the edited profile:
Even if you will try to set the execution policy, you will be blocked:
Set-ExecutionPolicy RemoteSigned
To overcome this issue, run the next command:
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
Of course that this setting is ONLY for the user you have logged in with.
- After running the command above, the date and time will be added correctly and without errors as a default:
Hope you will find it useful as I am :-)