Part 4: Customizing the PowerShell Prompt with Oh My Posh

In the previous articles of this series, you’ve learned how to create a PowerShell profile, organize your scripts, create aliases, import modules automatically, and build reusable functions. These customizations help create a more productive and personalized command-line experience.

Now it’s time to focus on one of the most visible elements of your PowerShell environment: the prompt.

Most users interact with the PowerShell prompt hundreds or even thousands of times each day. A well-designed prompt is more than just a location to type commands. It can act as a dashboard, providing valuable information about your current environment before you execute your next command.

A customized prompt can display information such as:

  • Your current working directory
  • Git repository and branch information
  • Administrator or elevated session status
  • Azure tenant and subscription details
  • Kubernetes cluster context
  • Current date and time
  • PowerShell version
  • Success or failure of the previous command
  • Execution duration of long-running commands
  • Active Python virtual environments
  • Remote session indicators

Instead of constantly running commands to check your environment, a good prompt presents the information you need exactly when you need it.

In this article, you’ll learn how PowerShell prompts work, how to build your own custom prompts, and how to use popular prompt customization frameworks such as Oh My PoshStarship, and posh-git to create professional and information-rich command-line experiences.

Why Customize Your Prompt?

The default PowerShell prompt is functional, but relatively basic.

By default, PowerShell displays something similar to:

PS C:\>

While simple and familiar, it doesn’t tell you much about your current environment.

Consider the following situations:

  • You’re working across multiple Azure subscriptions.
  • You’re switching between development and production environments.
  • You’re managing several Git repositories simultaneously.
  • You’re connected to remote systems.
  • You’re troubleshooting issues with elevated permissions.

In these scenarios, having contextual information directly in your prompt can help prevent mistakes and improve productivity.

Many system administrators, automation engineers, and developers customize their prompts specifically to reduce context switching and improve awareness of their working environment.

Understanding the PowerShell Prompt

The PowerShell prompt is controlled by a special function called prompt.

You can inspect the currently active prompt function using:

Get-Command prompt

To view the actual code behind the prompt:

(Get-Command prompt).Definition

You’ll notice that a prompt is simply a PowerShell function that returns a string.

Every time PowerShell finishes executing a command and is ready for new input, it runs this function and displays whatever text it returns.

Because it’s just PowerShell code, you can customize it in virtually any way you want.

Creating Your First Custom Prompt

Let’s start with the simplest possible example.

Add the following code to your PowerShell profile:

function prompt {
    "PS > "
}

Reload your profile:

. $PROFILE

The prompt now appears as:

PS >

This demonstrates the basic principle:

Whatever the prompt function returns becomes the prompt displayed on your screen.

Although simple, this concept opens the door to extensive customization.

Displaying the Current Directory

One of the most common prompt customizations is displaying the current path.

Add this prompt function:

function prompt {
    "PS [$($PWD.Path)] > "
}

Output:

PS [C:\Scripts] >

Now you always know exactly where you’re working.

However, you may notice a drawback when working in deeply nested directories:

PS [C:\Users\Casper\Documents\PowerShell\Projects\Automation\Scripts\Test] >

Long paths can make prompts difficult to read. Fortunately, PowerShell allows you to format and shorten information however you’d like.

Adding Color to Your Prompt

PowerShell prompts don’t have to be plain text.

You can use Write-Host to introduce colors:

function prompt {
    Write-Host "PS " -ForegroundColor Cyan -NoNewline
    Write-Host $PWD.Path -ForegroundColor Yellow -NoNewline
    return " > "
}

Example:

  • “PS” appears in cyan
  • Current path appears in yellow
  • Prompt symbol remains in the default color

This small change can significantly improve readability.

Displaying Administrator Status

It is often useful to know whether you’re running PowerShell with elevated permissions.

Here’s a simple example:

function prompt {

    $Identity = [Security.Principal.WindowsIdentity]::GetCurrent()
    $Principal = New-Object Security.Principal.WindowsPrincipal($Identity)

    $IsAdmin = $Principal.IsInRole(
        [Security.Principal.WindowsBuiltInRole]::Administrator
    )

    if ($IsAdmin) {
        "[ADMIN] PS $($PWD.Path)> "
    }
    else {
        "PS $($PWD.Path)> "
    }
}

Output:

[ADMIN] PS C:\Windows\System32>

This visual indicator helps prevent accidental changes on production systems or administrative workstations.

Showing Command Success or Failure

Another useful enhancement is displaying whether the previous command completed successfully.

PowerShell provides the automatic variable $?.

function prompt {

    if ($?) {
        $Status = "[OK]"
    }
    else {
        $Status = "[FAILED]"
    }

    "$Status PS $($PWD.Path)> "
}

Example:

[OK] PS C:\>

Or:

[FAILED] PS C:\>

This can be particularly useful when running scripts or lengthy automation tasks.

Building a More Practical Prompt

Combining several elements together creates a more useful prompt:

function prompt {

    $Time = Get-Date -Format "HH:mm:ss"

    "[$Time] PS $($PWD.Path)> "
}

Output:

[09:42:17] PS C:\Projects>

You can continue expanding this by including:

  • Username
  • Computer name
  • Git information
  • Azure subscriptions
  • PowerShell version
  • Connection state
  • Active virtual environments

However, as more information is added, prompt code can quickly become difficult to maintain.

This is exactly why modern prompt frameworks exist.

Introducing Oh My Posh

Oh My Posh is arguably the most popular prompt customization framework for PowerShell today.

Instead of writing and maintaining your own prompt logic, Oh My Posh provides:

  • Beautiful themes
  • Git integration
  • Azure integration
  • Kubernetes context awareness
  • Python virtual environment detection
  • WSL support
  • Cross-platform compatibility
  • PowerShell, Bash, Zsh, and Fish support

A typical Oh My Posh prompt might display:

▶ C:\Projects\Blog
  git: main ✓
  Azure: Production
  PowerShell 7.5.0

All accurately color-coded and visually organized.

Installing Oh My Posh

Installation is straightforward using WinGet:

winget install JanDeDobbeleer.OhMyPosh

Alternatively, you can install via Chocolatey:

choco install oh-my-posh -y

Verify the installation:

oh-my-posh version

Initializing Oh My Posh

Add the following to your PowerShell profile:

oh-my-posh init pwsh | Invoke-Expression

Reload your profile:

. $PROFILE

Oh My Posh will now load whenever PowerShell starts.

Choosing a Theme

One of the biggest advantages of Oh My Posh is the extensive collection of built-in themes.

List available themes:

Get-PoshThemes

Preview themes:

oh-my-posh get theme

Some popular themes include:

  • Paradox
  • Atomic
  • jandedobbeleer
  • M365Princess
  • PowerLevel10k Modern
  • Pure
  • Agnoster

To apply a specific theme:

oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\paradox.omp.json" | Invoke-Expression

Enhancing Git Integration with posh-git

Many PowerShell users combine Oh My Posh with posh-git.

Install it from the PowerShell Gallery:

Install-Module posh-git -Scope CurrentUser

Import it in your profile:

Import-Module posh-git

posh-git provides:

  • Git tab completion
  • Branch status indicators
  • Commit tracking
  • Repository awareness

When combined with Oh My Posh, Git information becomes an integrated part of your prompt.

Alternative: Starship

If you work across different shells and operating systems, Starship is another excellent choice.

Benefits include:

  • Extremely fast startup times
  • Single configuration file
  • Cross-platform support
  • Shell-independent configuration

Many users who switch frequently between PowerShell, Linux, and macOS environments prefer Starship because the same prompt configuration works everywhere.

Creating Your Own Oh My Posh Theme

While the built-in themes are excellent starting points, one of Oh My Posh’s greatest strengths is the ability to create your own theme.

Themes are defined using JSON and consist of segments. Each segment displays a specific piece of information, such as:

  • Current directory
  • Git status
  • Azure context
  • Kubernetes context
  • Execution time
  • Battery status
  • PowerShell version
  • System information

You can start with an existing theme and customize it to match your workflow.

Export a built-in theme:

oh-my-posh config export --output "$HOME\Documents\MyTheme.omp.json"

Then update your profile to load the new theme:

oh-my-posh init pwsh --config "$HOME\Documents\MyTheme.omp.json" | Invoke-Expression

Useing your Oh My Posh theme on all your devices

If you’re like me, you’ll want to use your own Oh My Posh theme on every one of your devices. The easiest way I’ve found to do this is to just upload the .omp.json file to github, all you have to do is install oh-my-posh and clone the repository on the machine and edit the powershell profile to include loading the theme and you’re all set!

For reference you can find my personal oh-my-posh theme on our GitHub.

Leave a Reply

Your email address will not be published. Required fields are marked *