如果在运行程序后损坏了Windows PowerShell的颜色,如何重置颜色?

我开始为使用node.js的Windows项目使用PowerShell。 当从Powershell命令行运行许多程序(包括node,supervisor和npm)时,我的PowerShell背景和前景色开始从默认的Powershell颜色改变。 我如何在PowerShell中保持一致的外观,以便轻松读取运行命令的结果?

我有MSBuild这个问题,特别是当我按Ctrl + C构build。 这是我放在我的profile.ps1文件中:

$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 } 

然后,我只是在MSBuild搞砸时调用Reset-Colors。

作为一次性操作,运行这个:

 > [Console]::ResetColor() 

从文档:(强调加)

前景色和背景色被恢复到当前处理开始时的颜色

首先在PowerShell中创build一个configuration文件,如果你还没有:

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

其次,把这个代码放在文件中:

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

第三,检查PowerShell是否允许你运行脚本。

 Get-ExecutionPolicy 

如果这说Restricted然后运行下面的ASpipe理员(请确保您了解安全含义):

 Set-ExecutionPolicy RemoteSigned 

打开一个新的PowerShell提示符,您应该可以运行节点或其他命令,而不会出现任何颜色问题。