Recently, a number of PowerShell MVPs were having a discussion about the transcript feature in Windows PowerShell. One comment that arose was a need to see how long tasks have run or otherwise provide some sort of date time information. One solution is to use a customized PowerShell prompt and provide the information yourself. Here are 3 ways.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
By default, PowerShell will display a prompt with PS and the current location. But you can create your own prompt at any time. All you need is your own version of a function called Prompt. This can be a simple one-liner like this.
[cc lang="PowerShell"]
Function Prompt {
"[{0} {1}]PS {2}>" -f (get-date).ToShortDateString(),(get-date).ToLongTimeString(),(get-location)
}
[/cc]
For testing purposes you can copy and paste the function into an open shell. Press Enter and you should get a new prompt like this:
[cc lang="PowerShell"]
[10/12/2010 9:14:56 AM]PS C:\Users\Jeff>
[/cc]
The function uses the -f formatting operator to construct a string with the current date as a short date, the current time as a long time and the current location. But you can put just about anything you want in a prompt. You can even use Write-Host. Here's a variation.
[cc lang="powershell"]
Function Prompt {
#[10/12/2010 8:41:47 AM]PS C:\Users\Jeff>
$dt= "[{0} {1}]" -f (get-date).toshortdatestring(),(get-date).TolongTimeString()
$loc="PS {0}" -f (get-location)
Write-Host $dt -NoNewline -ForegroundColor Yellow
write-host $loc -NoNewline
#when using Write-Host, the function has to write something to the pipeline
Write ">"
}
[/cc]
This version splits the prompt into two parts and uses Write-Host to display the date time in Yellow.
Or maybe, you need more granular information such as milliseconds. All we need to do is adjust the date time formatting.
[cc lang="PowerShell"]
Function Prompt {
#[10/12/2010 8:41:47 AM]PS C:\Users\Jeff>
$dt= "[{0:MM/dd/yyyy hh:mm:ss:ff}]" -f (get-date)
$loc="PS {0}" -f (get-location)
Write-Host $dt -NoNewline -ForegroundColor Yellow
write-host $loc -NoNewline
#to use Write-Host, the function has to write something to the pipeline
Write ">"
}
[/cc]
The only difference is using the -f operator to format the date time as one string that now includes milliseconds.
These prompts should work in both the traditional console as well as the ISE. Although you may want to adjust the color if using the latter.
Once you have found the prompt you want, add the function to your PowerShell profile. Then the next time you have PowerShell, you'll always have it. Download a text file with these prompts here.
It just seems weird that we’re making this more complex instead of simpler as compared with CMD “echo %date% %time%” To achieve that simplicity we have to create a cmdlet?
You can still do that in PowerShell with the Get-Date cmdlet. What I’m talking about is the Prompt function. In the CMD shell you can create a new prompt with the PROMPT command. In PowerShell you create a function called Prompt. The advantage really is that PowerShell gives us more options, even though it might take a little more code. In fact, you can’t do some of this from the CMD shell.
My first prompt is really a one line command. The variations are only a little longer because I wanted to be clear on what what going on. With a little planning and a small amount of effort, you can turn your prompt into a useful tool and not simply an announcement about where you are.