Syntax error prompt does not work when prompt function is defined in profile or launch command and uses Write-Host
See original GitHub issueEnvironment
# Tested in 3 environments
PS version: 5.1.18362.628
PSReadline version: 2.0.1
os: 10.0.18362.1 (WinBuild.160101.0800)
PS file version: 10.0.18362.1 (WinBuild.160101.0800)
HostName: ConsoleHost (Windows Terminal)
BufferWidth: 229
BufferHeight: 55
PS version: 7.0.1
PSReadline version: 2.0.0
os: 10.0.18362.1 (WinBuild.160101.0800)
PS file version: 7.0.1.0
HostName: ConsoleHost (Windows Terminal)
BufferWidth: 229
BufferHeight: 55
PS version: 7.0.0
PSReadline version: 2.0.0
os: Linux 4.4.0-18362-Microsoft #476-Microsoft Fri Nov 01 16:53:00 PST 2019 x86_64 x86_64 x86_64 GNU/Linux
PS file version: 7.0.0.0
HostName: ConsoleHost
BufferWidth: 229
BufferHeight: 55
Exception report
None
Steps to reproduce
Write a prompt
function that uses Write-Host
to construct the prompt. Place the prompt function in your PowerShell profile or in the -Command
option script block when launching PowerShell. Doing so will cause PSReadLine’s syntax error prompt notification color to not trigger.
# C:\users\<username>\Documents\WindowsPowerShell\profile.ps1
# Prompt function that uses Write-Host to construct prompt
function prompt {
Write-Host 'myprompt' -NoNewLine
'> ' # Need to also return string so PowerShell doesn't auto add "PS>"
}
Expected behavior
PS C:\> powershell.exe -NoLogo
PS C:\> function prompt { Write-Host 'myprompt' -NoNewLine; '> ' }
myprompt> } # syntax error prompt works
Actual behavior
PS C:\> powershell.exe -NoExit -Command { function prompt { Write-Host 'myprompt' -NoNewLine; '> ' } }
myprompt> } # last char of prompt does not change color with syntax error
All Examples
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (7 by maintainers)
Top Results From Across the Web
about Prompts - PowerShell
The default prompt appears only when the Prompt function generates an error or doesn't return an object. The default PowerShell prompt is: Copy....
Read more >it wont run because of a syntax error and i can't find it. sorry ...
it wont run because of a syntax error and i can't find it. sorry for ass code im new. (net script 2) :...
Read more >Write-Host (Microsoft.PowerShell.Utility)
Starting in Windows PowerShell 5.0, Write-Host is a wrapper for Write-Information This allows you to use Write-Host to emit output to the information...
Read more >Windows PowerShell: changing the command prompt
Just put the function prompt in your PowerShell profile ( notepad $PROFILE ), e.g.: function prompt {"PS: $(get-date)>"}. or colored:
Read more >PowerShell prompt to continue execution of code
Another simple solution would be to use: Read-Host -Prompt "Press any key to continue or CTRL+C to quit" | Out-Null.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Add the following to your profile - I think it should fix it:
Before v2, PSReadLine relied on screen scraping to implement the error prompt coloring. With the move to a more portable PSReadLine, screen scraping was no longer an option.
If you have a “pure” prompt, PSReadLine can infer what text needs to change to properly change the color, but if your prompt is not pure, e.g. calls
Write-Host
, then you need to help PSReadLine out with this configuration option.Note that if your prompt is extra fancy (like mine):
You can also specify the error coloring precisely by passing 2 strings instead of one - the first being the normal text, the second being the error text, so mine looks like:
Note how I use the background color instead of the foreground color.
@DecoyJoe - yeah, I see the same thing with the
?
but hadn’t investigated. It looks like you need to set the output encoding to UTF8:I thought PSReadLine was doing that for you, but maybe that changed. @daxian-dbw?