PowerShell provides a simple way to change the color of text in the console output using ANSI escape codes. By inserting these codes into your PowerShell scripts and commands, you can colorize text to help highlight important information, differentiate various output types, or just for aesthetic purposes.
Understanding ANSI Escape Codes
ANSI (American National Standards Institute) escape codes are special sequences of characters that control cursor location, text styling, and other output formatting in terminal applications.
For changing text color, the basic ANSI escape code format is:
\033[#;#m
Where the first # specifies the style (e.g. bold, underline) and the second # specifies the text color code.
Common Text Style Codes
Code | Effect |
---|---|
0 | Reset / Normal |
1 | Bold |
4 | Underline |
Common Text Color Codes
Code | Color |
---|---|
30 | Black |
31 | Red |
32 | Green |
33 | Yellow |
34 | Blue |
35 | Magenta |
36 | Cyan |
37 | White |
So by combining a style and color code together in the format \033[style;color;m you can produce colored text. For example:
- \033[1;31m = Bold Red
- \033[4;33m = Underline Yellow
- \033[1;37m = Bold White (bright color)
Changing Text Color in PowerShell
To actually use these ANSI escape codes to color text in PowerShell, you simply need to wrap the codes around the text strings you want to colorize.
Here is a simple example that prints some colored text to the console:
“`powershell
Write-Host “\033[31mThis text is red\033[0m”
Write-Host “\033[33mThis text is yellow\033[0m”
Write-Host “\033[32mThis text is green\033[0m”
“`
The \033[0m
code resets the text styling back to normal. This allows you to chain together sequences of differently colored text.
You can also store the escape codes in variables to reuse them multiple times:
“`powershell
$red = “\033[31m”
$yellow = “\033[33m”
$reset = “\033[0m”
Write-Host “${red}This is red text${reset}”
Write-Host “${yellow}This is yellow text${reset}”
“`
Colorizing Script Output
To add color to your actual PowerShell script output, you will need to insert the escape codes into strings and variables. For example:
“`powershell
# Output red text
“This is ${red}red${reset} text”
# Store colored strings in variables
$redText = “${red}Some red text${reset}”
$yellowText = “${yellow}Some yellow text${reset}”
# Output variables with colors
Write-Host “Here is red: $redText”
Write-Host “Here is yellow: $yellowText”
“`
You can also insert the codes directly into commands:
“`powershell
Write-Host “${red}This is a warning message!${reset}”
“`
Colorizing Status Output
For simple success/failure status messages, you may want to use green/red text:
“`powershell
if ($processFailed) {
Write-Host “${red}Process failed!${reset}”
} else {
Write-Host “${green}Process succeeded!${reset}”
}
“`
Colorizing Informational Output
To highlight key pieces of informational output, use colors to draw attention:
“`powershell
Write-Host “The ${red}important value${reset} is ${yellow}1${reset}.”
“`
Colorizing Headers
Add color to headers to visually separate different output sections:
“`powershell
Write-Host “${green}===== Users List =====${reset}”
# Output user list here
Write-Host “${blue}===== Disk Space =====${reset}”
# Output disk space here
“`
ANSI Escape Code Limitations
There are a couple limitations to be aware of when using ANSI escape codes in PowerShell:
- They only work in PowerShell console windows, not in ISE or debug console windows
- The must be escaped properly as PowerShell strings
- They add extra characters which can throw off string comparisons
- Only basic styling and colorizing is supported
So for production scripts, use colors sparingly when they will actually make output more readable or useful.
Alternative Options
If you need more advanced text formatting and styling, there are some PowerShell modules that can help:
PSColor Module
The PSColor module provides cmdlets for styling text and background colors, including piping support and easy to use aliases.
Pansies Module
The Pansies module provides PowerShell functions for ANSI formatting, ASCII art banners, and spinners for terminal output.
Terminal-Icons Module
The Terminal-Icons module lets you include glyphs and icons in the terminal output.
Conclusion
ANSI escape codes provide a simple way to add color formatting to PowerShell scripts directly from the terminal. By inserting code sequences you can colorize text, color code status messages, highlight headers, and draw attention to important output. While limited compared to GUI applications, it can help make raw terminal output more useful and readable.