Behind the PowerShell Pipeline logo

Behind the PowerShell Pipeline

Archives
Log in
Subscribe
September 15, 2026

PowerShell Prompt Potential

In this issue:

  • Adding DateTime
  • Shortened Paths
  • Emojis and Special Characters
  • Summary

Today's topic is a fun one, although it has practical applications as well. You look at it every day and many people don't give it a second thought. I'm talking about your PowerShell prompt. This is the text displayed to the left of the cursor in your PowerShell console.

PS C:\>

The display is defined by special PowerShell function called prompt. This is a reserved function word. When you start PowerShell, it defines a default prompt function.

PS C:\> Get-Content function:prompt

"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) ";
# .Link
# https://go.microsoft.com/fwlink/?LinkID=225750
# .ExternalHelp System.Management.Automation.dll-help.xml

Here's the fun part. You can define a custom prompt function as long as it is named prompt. I thought we'd look at some options you might consider adding to your prompt function.

Adding DateTime

The prompt function runs every time you press enter. This means all code in the prompt function runs every time. The implication here is that you don't want any long-running code in your prompt function. However, display a datetime is very easy.

function prompt {
    $nested = $('>' * ($nestedPromptLevel + 1))
    "$(Get-Date -Format g) | PS $($pwd.Path)$nested "
}

I'm going to keep the nested code. $NestedPromptLevel is an automatic variable. I've also replaced $executionContext.SessionState.Path.CurrentLocation with the built-in $pwd variable. I have yet to encounter a situation where $executionContext was required. Using $pwd simplifies the code.

In this function I am inserting a date value. To use, all I need to do is to paste the function into my PowerShell session. Or dot source a script file with the function. The change is immediate as soon as I press Enter.

9/15/2026 11:37 AM | PS C:\> $PSVersionTable.PSVersion.ToString()
7.6.6
9/15/2026 11:38 AM | PS C:\>

Or maybe you only need to see the time.

function prompt {
    $nested = $('>' * ($nestedPromptLevel + 1))
    "$(Get-Date -Format T) | PS $($pwd.Path)$nested "
}

The time shown is the last time you pressed Enter.

11:40:36 AM | PS C:\> get-process -id $pid

 NPM(K)    PM(M)      WS(M)     CPU(s)      Id  SI ProcessName
 ------    -----      -----     ------      --  -- -----------
     85    53.24     133.96       5.05   16664   1 pwsh

11:41:01 AM | PS C:\>

It is important to remember that the prompt function must write something to the pipeline, even if it is a single space. In my functions, it is writing a string.

Want to read the full issue?
Already a paid subscriber? Click here to log in.
GitHub
Bluesky
LinkedIn
Mastodon
jdhitsolutions.github.io
Powered by Buttondown, the easiest way to start and grow your newsletter.