PowerTip: Change PowerShell ISE Foreground Color


Summary: Programatically change the foreground color in the Windows PowerShell ISE.

Hey, Scripting Guy! Question How can I change the foreground color in the Windows PowerShell ISE?

Hey, Scripting Guy! Answer Use $Host.UI.RawUI.ForegroundColor and assign a new color. For example, to change it to cyan, use:

$Host.UI.RawUI.ForegroundColor = 'cyan'

Comments (4)

  1. Jason says:

    Is there a command to change colors back to the default once changed?

  2. Jeff25 says:

    I was wondering the same thing? some easy command to revert back to default?

  3. To do that you would first need to capture the current values like this:
    # Check to see if either the foreground or background colors were specified to change, and if so change them for the duration of the function.
    If ($ForegroundColor) {
    $CurrentForegroundColor = $Host.UI.RawUI.ForegroundColor
    $Host.UI.RawUI.ForegroundColor = $ForegroundColor
    }
    If ($BackgroundColor) {
    $CurrentBackgroundColor = $Host.UI.RawUI.BackgroundColor
    $Host.UI.RawUI.BackgroundColor = $BackgroundColor
    }

    Then perform whatever you wanted to do, and then put them back when you are done with this code:

    # Return colors to their previous state if they were changed.
    If ($CurrentForegroundColor) {
    $Host.UI.RawUI.ForegroundColor = $CurrentForegroundColor
    }
    If ($CurrentBackgroundColor) {
    $Host.UI.RawUI.BackgroundColor = $CurrentBackgroundColor
    }
    }

    1. I should note that changing the ForegroundColor in the PowerShell ISE changes the color of ALL foreground text, and that putting it back reverts the color change of ALL foreground text as well, not just what you output to the screen during the change. This is not the same behavior as the native PowerShell window where it will only change the color of what is output to the screen after the color is changed, versus all text on the screen.

Skip to main content