3

Out of the box, running PowerShell with Cmder sets the foreground color to white:

enter image description here

Today I installed PowerShell Core and created a new Cmder task exactly like the default 'PowerShell as Admin task', but points to pwsh.exe instead of Powershell.exe:

*C:\Program Files\PowerShell\6\pwsh.exe -ExecutionPolicy Bypass -NoLogo -NoProfile -NoExit -Command "Invoke-Expression '. ''%ConEmuDir%\..\profile.ps1'''"

For reasons I don't understand, the foreground color for the PowerShell Core window is green:

enter image description here

While trying to understand why, I spent some time fiddling with the following block of code contained in \vendor\profile.ps1:

[ScriptBlock]$CmderPrompt = { $Host.UI.RawUI.ForegroundColor = "White" Write-Host $pwd.ProviderPath -NoNewLine -ForegroundColor Green checkGit($pwd.ProviderPath) }

Here's where it gets weird. If I change that Write-Host statement to use -ForegroundColor White instead of Green, the foreground color remains permanently white. If I change it to Red, the foreground color remains permanently red. Even weirder: If I leave the original Write-Host statement alone (using the original Green color), but add an additional Write-Host statement above it without a Foregroundcolor parameter, the foreground color remains white. It's as though a Write-Host statement is required in order to maintain the state of $Host.UI.RawUI.ForegroundColor.

Anyone else experiencing this oddity? Am I missing something stupid?

I tried this with Cmder 1.3.5 and 1.3.6 - same result.


Additional Examples:

Results in white foreground color:

[ScriptBlock]$CmderPrompt = { $Host.UI.RawUI.ForegroundColor = "White" Write-Host $pwd.ProviderPath -NoNewLine -ForegroundColor White checkGit($pwd.ProviderPath) }

Also results in white foreground color:

[ScriptBlock]$CmderPrompt = { $Host.UI.RawUI.ForegroundColor = "White" Write-Host "blah" Write-Host $pwd.ProviderPath -NoNewLine -ForegroundColor Green checkGit($pwd.ProviderPath) }

Results in red foreground color:

[ScriptBlock]$CmderPrompt = { $Host.UI.RawUI.ForegroundColor = "White" Write-Host "blah" -ForegroundColor Red Write-Host $pwd.ProviderPath -NoNewLine -ForegroundColor Green checkGit($pwd.ProviderPath) }

5
  • 1
    Report this to cmder authors
    – Maximus
    Sep 24, 2018 at 7:27
  • I am also experiencing this issue. I use a portable version of cmder on two different computers. On both of them, I use pwsh 6.1.0, but weirdly enough only one of them has this foreground color issue.
    – Manuzor
    Oct 2, 2018 at 5:46
  • Note that I did open an issue about this on the Cmder github repo. Oct 3, 2018 at 20:44
  • This is a problem for me as of the Windows 10 v1809 update. Effecting two separate machines.
    – root
    Oct 4, 2018 at 21:45
  • Yeah, I just updated to the latest Windows 10 and now this is an issue with both PS Core and PS 5. Oct 6, 2018 at 21:22

2 Answers 2

2

As mentioned in my comment to the question, I am also experiencing this issue. I did not exactly find out why this is happening, but I have found a workaround.

The OP inspired me to poke around with Write-Host as well, so I've come up with this:

# File "$env:CMDER_ROOT\config\user-profile.ps1"
# I copy-pasted this from "$env:CMDER_ROOT\vendor\profile.ps1" and added the `b line.
[ScriptBlock]$CmderPrompt = {
    $Host.UI.RawUI.ForegroundColor = "White"

    # Workaround to make above line apply the "White" foreground color.
    # Seems like you have to print _something_ before using Write-Host with -ForegroundColor.
    # Note: Empty string "" doesn't work.
    Write-Host "`r" -NoNewline

    Microsoft.PowerShell.Utility\Write-Host $pwd.ProviderPath -NoNewLine -ForegroundColor Green
    Microsoft.PowerShell.Utility\Write-Host (checkGit($pwd.ProviderPath)) -NoNewLine 
}

This works for me because I know that the cursor is at the beginning of a line at that point, so `r doens't do anything. Not pretty but it's a quick fix that works. I hope it does for others, too.

0
0

I have made a simpler workaround that will work until a known fix is made.

  1. Open the default cmd session.
  2. write powershell command.

Now you are in powershell but with all the default colors of cmd. Should be a great/easy solution if you need to open 2 split tabs. If you are lazy to write the powershell command, just create another cmd session with a start-up with the powershell command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.