SnapShot
SnapShot

Reputation: 5564

How can you reset the colors for Windows PowerShell if they become corrupted after running a program?

I am starting to use PowerShell for a Windows project using node.js. When running many programs (including node, supervisor and npm) from the Powershell commmand line, my PowerShell background and foreground colors start to change from the default Powershell colors. How can I maintain a consistent look within PowerShell so I can easily read the results of running commands?

Upvotes: 28

Views: 13421

Answers (4)

Tomoyuki Aota
Tomoyuki Aota

Reputation: 937

I fix the color issue by running the following commands:

cmd
color 07
exit

What these commands do is to run cmd in PowerShell, run color 07 to fix the color, and exit from cmd to revert to PowerShell.

Upvotes: 1

Shaun Luttin
Shaun Luttin

Reputation: 141792

As a one-time operation just run this:

> [Console]::ResetColor()

From the docs: (emphasis added)

The foreground and background colors are restored to the colors that existed when the current process began.

Upvotes: 73

SnapShot
SnapShot

Reputation: 5564

First create a profile in PowerShell if you do not have one already:

test-path $profile
new-item -path $profile -itemtype file -force
notepad $profile

Second, put this code in the file:

function prompt {
  [Console]::ResetColor()
}

Third, check if PowerShell will allow you to run scripts.

Get-ExecutionPolicy

If this says Restricted then run the following AS Administrator (please be sure you understand the security implications):

Set-ExecutionPolicy RemoteSigned

Open up a new PowerShell prompt and you should be able run node or other commands without any color issues.

Upvotes: 13

Keith Hill
Keith Hill

Reputation: 202102

I have this problem with MSBuild especially when I ctrl+C a build. This is what I put in my profile.ps1 file:

$OrigBgColor = $host.ui.rawui.BackgroundColor
$OrigFgColor = $host.ui.rawui.ForegroundColor

# MSBUILD has a nasty habit of leaving the foreground color red
# if you Ctrl+C while it is outputting errors.
function Reset-Colors {
    $host.ui.rawui.BackgroundColor = $OrigBgColor
    $host.ui.rawui.ForegroundColor = $OrigFgColor
}

Then I just invoke Reset-Colors when MSBuild has messed them up.

Upvotes: 17

Related Questions