powershellsyntaxprompt

Powershell prompt, red trailing character on error



I am trying to create my own Powershell prompt in $profile.

For now, i have the following :

$ESC = [char]27
$RESET = "$ESC[0m"
$pwd = $executionContext.SessionState.Path.CurrentLocation

function prompt {
  $fontColor = "2;192;192;192m"
  $backColor = "2;64;96;128m"
  $shdwColor = "2;32;48;64m"

  $main   = "$RESET$ESC[38;$fontColor$ESC[48;$backColor"
  $shadow = "$RESET$ESC[38;$backColor$ESC[48;$shdwColor"
  $end    = "$RESET$ESC[38;$shdwColor"

  #      "$main $pwd  $shadow $end ";
  #      "$main $pwd  $shadow $end$RESET ";
  #      "$main $pwd  $shadow $end$ESC[0m ";
  #      "$main $pwd  $shadow $end$ESC[0 ";
  #      "$main $pwd  $shadow $end $RESET";
  #      "$main $pwd  $shadow $end $ESC[0m";
  return "$main $pwd  $shadow $end $ESC[0";
}

Which render as follow :
Powershell profile as follow : C:\Windows\System32 / /

When i type ls or dir (for example), everything work fine.

However, when i type a string, my trailing zero (from $ESC[0) appear as red, and when i close the string, the zero change color to white.
Powershell prompt with a "0" appearing

This also appear when i put $RESET or $ESC[0m instead of $ESC[0 (but with a "m" instead of "0"), and when i remove them it's the that appear red...
Same image as above, but with  as trailing char

We can also see that in the default Powershell prompt, the ">" become red when we open a string :
Default Powershell prompt with red ">" on string opening

Has someone ever experienced the same issue in prompt making, and if yes, how did you remove this coloring or how did you adapt ?

Thanks !


Solution

  • It is because of the PSReadLine module. The solution is to empty the set of characters that change to red when there is a parsing error in the command being entered before the user presses Enter:

    Set-PSReadLineOption -PromptText ''
    

    You can read more on the Microsoft Learn Website Set-PSReadLineOption