PowerShell で色を変更する方法

  1. PowerShell で色を変更するための紹介
  2. PowerShell で [System.Enum] クラスを使用する
  3. PowerShell でコンソールの色を変更する
PowerShell で色を変更する方法

この記事では、PowerShell を使用してフォントの色、スクリプトの背景色、およびコンソールウィンドウの色を変更する方法について説明します。

PowerShell で色を変更するための紹介

これらのコマンドは、PowerShell コンソール、コンソールホストに関する情報を持つオブジェクトを取得します。

コマンド:

$host

出力:

Name             : Windows PowerShell ISE Host
Version          : 5.1.22000.282
InstanceId       : 8cff2bea-868b-4d9e-b55a-06a3f4b8c20c
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-PH
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.Host.ISE.ISEOptions
DebuggerEnabled  : True
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace

PrivateData プロパティには、私たちが探しているすべての色プロパティがあります。

コマンド:

$host.PrivateData

出力:

ErrorForegroundColor                      : #FFFF9494
ErrorBackgroundColor                      : #00FFFFFF
WarningForegroundColor                    : #FFFF8C00
WarningBackgroundColor                    : #00FFFFFF
VerboseForegroundColor                    : #FF00FFFF
VerboseBackgroundColor                    : #00FFFFFF
DebugForegroundColor                      : #FF00FFFF
DebugBackgroundColor                      : #00FFFFFF
ConsolePaneBackgroundColor                : #FF012456
ConsolePaneTextBackgroundColor            : #FF012456
ConsolePaneForegroundColor                : #FFF5F5F5
ScriptPaneBackgroundColor                 : #FFFFFFFF
ScriptPaneForegroundColor                 : #FF000000

色は、 $host.PrivateData オブジェクト内の警告、エラー、デバッグ、詳細、および進行ストリームのために設定されています。これらの値の 1つを変更して、コンソールの色も変わるかどうかを見てみてください。

コマンド:

$host.PrivateData.ErrorBackgroundColor = "White"

出力:

Black
DarkBlue
DarkGreen
DarkCyan
DarkRed
DarkMagenta
DarkYellow
Gray
DarkGray
Blue
Green
Cyan
Red
Magenta
Yellow
White

コンソールに表示される出力は、通常の白いテキスト(デフォルトの色が変更されていない場合)です。次のコマンドを実行して、コンソールの色をそれぞれの色として表示します。

コマンド:

[System.Enum]::GetValues('ConsoleColor') |
    ForEach-Object { Write-Host $_ -ForegroundColor $_ }

出力:

フォントカラーを使用して色の値を表示するには、Write-Host を使います。

色の組み合わせがどのように機能し、私たちの好みに合うかを確認したいとします。その場合、次のコマンドを使用して、すべての可能な背景色に対するすべての可能な前景色を出力できます。

コマンド:

$colors = [enum]::GetValues([System.ConsoleColor])
Foreach ($bgcolor in $colors) {
    Foreach ($fgcolor in $colors) {
        Write-Host "$fgcolor|"  -ForegroundColor $fgcolor -BackgroundColor $bgcolor -NoNewline
    }
    Write-Host " on $bgcolor"
}

出力:

コンソールの前景と背景の色のすべての可能な組み合わせを表示する

PowerShell でコンソールの色を変更する

目立つコンソールの前景色と背景色を変更することは、以前のオブジェクトとは少し異なります。テキストの色と PowerShell のコンソールウィンドウを変更するために、以下のスニペットを試してみることができます。

コマンド:

$host.UI.RawUI.ForegroundColor = "DarkGreen"
$host.UI.RawUI.BackgroundColor = "Black"
cls

出力:

PowerShell でコンソールの色を変更する

チュートリアルを楽しんでいますか? <a href="https://www.youtube.com/@delftstack/?sub_confirmation=1" style="color: #a94442; font-weight: bold; text-decoration: underline;">DelftStackをチャンネル登録</a> して、高品質な動画ガイドをさらに制作するためのサポートをお願いします。 Subscribe
Marion Paul Kenneth Mendoza avatar Marion Paul Kenneth Mendoza avatar

Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.

LinkedIn

関連記事 - PowerShell Color