PowerShell Customizing PowerShell Prompt & Profile Tips

  • Welcome to ITBible, we're your #1 resource for enterprise or homelab IT problems (or just a place to show off your stuff).

Andy

OG
ITB-OG
Security Engineer
Apr 6, 2023
28
7
68
Texas
I have a thing about command line prompts. I like tweaking and customizing. The prompt is such a simple thing, but impacts experience dramatically. Once I learned about prompt customization, I found a general format that works for me and can be replicated across multiple environments and operating systems.

The below code snippet comes from my PowerShell profile. PowerShell profiles are a bit of a complex topic, especially if you haven't worked with them before. More on this later.

Quick-and-dirty PS Profile path:
Code:
$Home\[My ]Documents\WindowsPowerShell\Profile.ps1


PowerShell:
Function Prompt {
        $location = (Get-Location) | Split-Path -Leaf
        $time = Get-Date -Format "HH:mm:ss"
        Write-Host "$time " -ForegroundColor DarkGray -NoNewline
        Write-Host "[" -ForegroundColor White -NoNewline
        Write-Host "$location" -ForegroundColor DarkRed -NoNewline
        Write-Host "]:" -ForegroundColor White -NoNewline
        " "
}

Let's pivot on that profile complexity issue. PowerShell profiles are necessary for a customized terminal experience. When the shell process is launched, it loads PS profiles from the various locations where they can be stored. The order in which the profiles are loaded is important, as it can impact which customizations take precedence over others. If no Profile.ps1 file is present, the loader simply skips to the next possible location.

Here's a quick rundown of the order in which PowerShell profiles are loaded:
  1. All Users, All Hosts - $PsHome\Profile.ps1
  2. All Users, Current Host - $PsHome\Microsoft.PowerShell_profile.ps1
  3. Current User, All Hosts - $Home[My]Documents\WindowsPowerShell\Profile.ps1
  4. Current User, Current Host - $Home[My]Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
The snippet I provided ideally should be added to the "Current User, All Hosts" profile (option 3 above). This scopes the customizations to the logged in user and applies to all PowerShell instances run by that user. This security control is in place partly to help prevent data leakage, which can occur when customized profiles contain secrets or sensitive data. That happens far more frequently than one might assume.

In addition to the prompt customizations that I have configured, there are plenty of additional ways to tailor the PowerShell experience with profiles:
  1. Add custom functions or aliases for frequently used commands.
  2. Set environment variables for your session.
  3. Load specific PowerShell modules at startup.
Feel free to experiment and find what works best for you. A carefully-customized terminal can significantly improve daily CLI usage and drive efficient practices.

I would love to hear about your experiences with customizing PowerShell to your liking.
 
  • Like
Reactions: WizardTux