How to Change Error Message Colors in Windows 10 PowerShell Console

While this was a really easy way to change some of the settings, what if you want to do more extensive changes to your PowerShell console? For example, what if you want to change the text and background colors of error messages from red on black to something a little less drastic? For that, you’ll need to get down and dirty and use the console itself rather than rely on GUI. First off, to know the default colors, go ahead and copy/paste this code on the PowerShell console and press ‘Enter’: $host.privatedata

To change the foreground and background colors of error messages, all you need to do is assign new values. Since I’d like to change the background color to ‘Magenta’ and foreground color to ‘Green’, I’ll input the two commands below. Just remember to enter them separately and press ‘Enter’ in each case.
$host.PrivateData.ErrorBackgroundColor = "Magenta"
$host.PrivateData.ErrorForegroundColor = "Green"

You have now configured your console settings, but you’ll need to save them to your profile settings so that the screen opens up exactly as you want it to, every time. For that, first run the command $profile. This will show you the name (and location) of the default file for your profile.

In reality, though, the default PowerShell configuration file does not even exist in most cases. So run the following command to check if it already exists or if you need to create it from scratch: test-path $profile. If the file already exists, you’ll get a “True” output, else, you’ll get “False”.

As you can see from the above screenshot, I got the latter, so I’ll need to create the file. If you get “True”, skip this step and go to the next. Else, enter the following command to create the file: New-Item -path $profile -type file -force

Once the file is created, you can easily edit it with Notepad by using the notepad $profile command in the Powershell Window. From there, you can add whatever configuration code you want to using the commands discussed above. You can not only change colors, but also fonts, windows size, etc through this method, but we’re only going to take a look at changing colors without complicating matters any further.

$console = $host.ui.rawui
$console.backgroundcolor = "black"
$console.foregroundcolor = "white"
$colors = $host.privatedata
$colors.verbosebackgroundcolor = "Magenta"
$colors.verboseforegroundcolor = "Green"
$colors.warningbackgroundcolor = "Red"
$colors.warningforegroundcolor = "white"
$colors.ErrorBackgroundColor = "DarkCyan"
$colors.ErrorForegroundColor = "Yellow"
set-location C:\
clear-host

We’re almost there, but there’s one last step. Run the following command to permit local scripts to run on your system: Set-ExecutionPolicy RemoteSigned and select “A” to allow all scripts. Now that you’re done, this is how your PowerShell console would look every time you start it up. Even the errors messages would look a little less jarring than they normally do.

That’s it, folks, I’ve made the Windows PowerShell console on my work laptop look almost exactly like the good old Command Prompt with just a dash of color thrown in for fun.

Ref:https://beebom.com/how-change-powershell-color-scheme-windows-10/

上一篇:开发者常用的 Sublime Text 3 插件


下一篇:nowcoder172A 中位数 (二分答案)