windows terminal prompt image

In PowerShell, you can use the ESC[<n>m sequence to set the format of the screen and text. The <n> number represents different formatting modes.

Reference: Text Formatting

$([char]27) is an ASCII character representing an escape character, and the code 0 for <n> will reset all attributes to the default state.

Code 32 will set Foreground to Green.

echo "$([char]27)[32m Green Colour text $([char]27)[0m"

Foreground Green Colour text

To see all the available colours run the following powershell code:

$Colors = '30','31','32','33','34','35','36','37','38','39','90','91','92','93','94','95','96','97'
foreach ($Color in $Colors)
{
    echo "$([char]27)[$($Color)m colour code $($Color) $([char]27)[0m"
}

All Foreground Colours

The “`r`n” sequence is eqivalent to \n newline character. Environment variable $env:UserName and $env:COMPUTERNAME will print the username and computer name.

additionaly we can set the variable for $([char]27).

$ESC = [char]27

To test this, run the following command:

echo "`r`n$ESC[32m┌──PS($ESC[94m$env:UserName@$env:COMPUTERNAME$ESC[32m)-[$ESC[0m$(Get-Location)$ESC[32m]`r`n└─$ESC[94m$ $ESC[0m"

Test Custom Prompt

Now use the prompt function in PowerShell to set the command prompt prefix.

$ESC = [char]27
function prompt { "`r`n$ESC[32m┌──PS($ESC[94m$env:UserName@$env:COMPUTERNAME$ESC[32m)-[$ESC[0m$(Get-Location)$ESC[32m]`r`n└─$ESC[94m$ $ESC[0m" }

Set Custom Prompt

That’s it, Now we have a Kali Linux-like command prompt.

Additionally, you can add it to the PowerShell profile path to make it run at startup.

To get the profile path run:

$PROFILE.AllUsersAllHosts

If you want to replace the Current User’s Home directory with the ~ sign in the path, use the following command:

$ESC = [char]27
function prompt { "`r`n$ESC[32m┌──PS($ESC[94m$env:UserName@$env:COMPUTERNAME$ESC$ESC[32m)-[$ESC[0m$($(Get-Location) -replace "^$([regex]::Escape($HOME+"\"))", '~\' -replace "^$([regex]::Escape($HOME))", '~')$ESC[32m]`r`n└─$ESC[94m$ $ESC[0m" }

Custom Prompt with ~ sign


Happy Hacking