以编程方式更改PowerShell的16种默认控制台颜色 [英] Programmatically change PowerShell's 16 default console colours

查看:261
本文介绍了以编程方式更改PowerShell的16种默认控制台颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PowerShell通过为其16种控制台颜色之一(由于某种原因称为DarkMagenta的一种-)分配蓝色值,改进了(坦率)丑陋的Windows控制台颜色方案,并将其用作默认屏幕背景。 / p>

我想以编程方式将16种颜色中的每一种更改为自定义配色方案。例如,在我的PowerShell配置文件中。



我找到了有关如何更改ConsoleHost的16种颜色之一分配给不同类型文本的说明,但是我实际需要将ConsoleHost的16种颜色中的每一种更改为不同的十六进制值。 (仅针对我自己,不适用于其他用户或其他控制台,例如 cmd.exe 。)



当然,这可以通过右键单击菜单栏并手动调整属性或默认设置来完成,但这很快就很累人。而且我无法找到这些设置的保留位置。



(我已经搜索了注册表,并且 C:\Users\< current_user> \AppData ,什么都没找到,除了 HKCU:\控制台 ColorTable ### 属性c $ c>,其范围显然比PowerShell控制台设置更广。)



如果有人可以提供帮助,将不胜感激。

解决方案

控制台颜色在多个位置定义:




  • 全局/默认 HKCU:\控制台。这适用于所有 conhost.exe -应用程序,包括 cmd.exe powershell.exe

  • 每个进程: HKCU:\控制台\< PROCESS_PATH_WITH_UNDESCORE> 用于特定于流程的更改。例如 HKEY_CURRENT_USER\控制台\%SystemRoot%_System32_WindowsPowerShell_v1.0_powershell.exe 已定义并已修改 ColorTable05 ColorTable06

  • 每个快捷方式:快捷方式(.lnk)内。这很难以编程方式进行修改,可能需要P\Invoke。



您可以使用以下命令使用PS修改过程级别的值:

  Set-ItemProperty -Path HKCU:\Console\%SystemRoot%_System32_WindowsPowerShell_v1.0_powershell.exe-名称ColorTable04-值5645313 

请注意,要查看流程级别的值,您需要使用run启动PS, Windows资源管理器等。如果使用快捷方式之一,则将使用快捷方式的值。因此,修改快捷方式并为新设置保留快捷方式可能会更容易。



每个 ColorTable的名称都硬编码在 System.ConsoleColor枚举,所以它只是随机他们使用了一种叫做DarkMagneta的东西可能是因为它是一种使用很少的独特颜色。


PowerShell improves on the (frankly) fugly Windows Console colour scheme by assigning a bluish value to one of its 16 console colours ( - the one known as DarkMagenta for some reason - ), and uses this as the default screen background.

I want to programmatically change each of the 16 colours to a custom colour scheme. In my PowerShell profile, for example.

I've found explanations on how to change which one of the ConsoleHost's 16 colours gets assigned to different kinds of text, but what I want is to actually change each of the ConsoleHost's 16 colours to a different hex value. (For myself only, not for other users, or other consoles, such as cmd.exe.)

Of course this can be done by right-clicking the menu bar and manually adjusting the "Properties" or "Default" settings, but this gets very quickly tiring. And I cannot locate where these settings get persisted.

(I have already searched the registry and C:\Users\<current_user>\AppData, and found nothing, except for the ColorTable## properties under HKCU:\Console, whose scope is obviously broader than the PowerShell console settings.)

If anyone can help, it would be greatly appreciated.

解决方案

The console colors are defined in multiple places:

  • Global/Default: HKCU:\Console. This applies to all conhost.exe-applications including cmd.exe and powershell.exe.
  • Per process: HKCU:\Console\<PROCESS_PATH_WITH_UNDESCORE> for process-specific changes. Ex. HKEY_CURRENT_USER\Console\%SystemRoot%_System32_WindowsPowerShell_v1.0_powershell.exe is defined and has modified ColorTable05 and ColorTable06.
  • Per shortcut: Inside the shortcut (.lnk). This is hard to modify programmatically, would probably require P\Invoke.

You can modify the process-level values with PS using:

Set-ItemProperty -Path "HKCU:\Console\%SystemRoot%_System32_WindowsPowerShell_v1.0_powershell.exe" -Name ColorTable04 -Value 5645313

Be aware that to see the values of the process-level you need start PS using run, windows explorer etc. If you use one of the shortcuts, then the shortcut's values will be used. So it might be easier to modify the shortcut and keep a copy of it for new setups.

Each "ColorTable" has a name hardcoded in the System.ConsoleColor-enum, so it's just "random" that they used the one called DarkMagneta. Probably because it's a unique color that isn't used that much.

这篇关于以编程方式更改PowerShell的16种默认控制台颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆