PowerShell profile prompt customization¶
PowerShell profiles allow users to customize the command-line prompt, which defines the appearance and behavior of the command-line interface.^[windows.md]
Profile location¶
Customizations are typically stored in the user's PowerShell profile file. For PowerShell 7, this file is located at C:\Program Files\PowerShell\7\Profile.ps1.^[windows.md]
Prompt function¶
The primary mechanism for customization is overriding the prompt function.^[windows.md]
A basic implementation constructs a string containing the current location and nesting level indicators:
powershell
function prompt {
$loc = $($executionContext.SessionState.Path.CurrentLocation);
$out = "PS $loc$('>' * ($nestedPromptLevel + 1)) ";
return $out
}^[windows.md]
Terminal integration¶
Custom prompts can integrate with specific terminal features, such as Windows Terminal, to enable functionality like opening new tabs in the current working directory^[windows.md].
This is achieved by embedding ANSI escape codes into the returned prompt string^[windows.md]:
powershell
$out += "$([char]27)]9;9;`"$loc`"$([char]27)\"^[windows.md]
More advanced implementations can check the current provider (e.g., FileSystem) to apply these path codes conditionally^[windows.md]:
powershell
function prompt
{
$loc = Get-Location
$prompt = & $GitPromptScriptBlock
$prompt += "$([char]27)]9;12$([char]7)"
if ($loc.Provider.Name -eq "FileSystem")
{
$prompt += "$([char]27)]9;9;`"$($loc.Path)`"$([char]7)"
}
$prompt
}^[windows.md]
Related concepts¶
- [[PowerShell]]
- [[ANSI escape codes]]
- [[Windows Terminal]]
Sources¶
^[windows.md]