【问题标题】:How to set the default color of console output in the Powershell profile如何在 Powershell 配置文件中设置控制台输出的默认颜色
【发布时间】:2021-07-27 14:22:43
【问题描述】:

我一直在玩 Set-PSReadLineOption 颜色。我终于在我的 Windows PowerShell 配置文件中获得了一个我喜欢的深色主题控制台(除了控制台窗口的背景,它似乎只能手动设置)。

我想做的最后一个更改是为通用命令(例如 gci)的输出设置颜色

131|PS(5.1.18362) C:\Users\....\wsPowerShell\Modules [210505-11:01:24]> gci


    Directory: C:\Users\User\Documents\WindowsPowerShell\Modules   #Color this line


Mode                LastWriteTime         Length Name              #Color this line
----                -------------         ------ ----              #Color this line
d-----       10/21/2020   2:41 PM                PSScriptAnalyzer  #Color this line

目前通用命令输出的默认颜色是白色,我更喜欢浅灰色或深绿色。我只知道如何通过首选项菜单将其更改为屏幕文本。

有没有办法在 Windows PowerShell 配置文件中自定义它?如果无法为整个输出着色,我也可以为输出的部分着色。

谢谢

【问题讨论】:

    标签: powershell


    【解决方案1】:

    您可以为当前用户创建配置文件,每次打开控制台时都会加载该配置文件。 要检查您的配置文件路径,请使用自动变量$profile

    PS C:\Users\myuser> $profile
    C:\Users\myuser\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
    

    如果文件存在,则可以修改它,如果不存在,则必须先创建它。 现在你可以配置你想要的了,例如:

    $Host.ui.rawui.backgroundcolor = "white"
    $Host.ui.rawui.foregroundcolor = "black"
    

    参考:https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles

    通过这种方式,您可以修改文本的属性,但不能修改控制台背景本身。只能在 ISE 中使用参数来完成,在普通控制台中您没有要使用的参数。

    在 ISE 配置文件中:

    $Host.PrivateData.ConsolePaneBackgroundColor = "black"
    

    有一些着色参数,你也可以在普通控制台中更改:

    PS C:\Users\myuser> $host.PrivateData
    
    ErrorForegroundColor    : Red
    ErrorBackgroundColor    : Black
    WarningForegroundColor  : Yellow
    WarningBackgroundColor  : Black
    DebugForegroundColor    : Yellow
    DebugBackgroundColor    : Black
    VerboseForegroundColor  : Yellow
    VerboseBackgroundColor  : Black
    ProgressForegroundColor : Yellow
    ProgressBackgroundColor : DarkCyan
    

    根据您的需要,将以下内容添加到您的个人资料中:

    cmd /c color 0f
    

    (它在黑色背景上生成亮白色文本 - 当然您可以修改参数) 参考:https://ss64.com/nt/color.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-14
      • 2016-04-10
      • 2022-01-24
      • 2023-01-13
      • 1970-01-01
      • 2021-05-24
      • 2016-01-16
      • 2010-12-04
      相关资源
      最近更新 更多