Understanding ANSI Escape Sequences in Windows: Examples in PowerShell and Batch Scripts
ANSI escape sequences are a powerful tool for controlling text formatting and color in command-line interfaces. While primarily associated with Unix-based systems, Windows also supports ANSI escape sequences, allowing users to enhance the visual appeal and functionality of their PowerShell and Batch scripts. In this article, we will explore the usage of ANSI escape sequences in Windows, with a focus on practical examples in PowerShell and Batch scripts.
Examples:
1. Changing Text Color in PowerShell:
To change the text color in PowerShell using ANSI escape sequences, you can use the following code snippet:
powershell
Write-Host "`e[31mThis text will be displayed in red.`e[0m"
batch
@echo off
echo This is `e[1mbold`e[0m text.
echo This is `e[4munderlined`e[0m text.
echo This is `e[7minverted`e[0m text.
powershell
$menu = @"
`e[32m1. Option 1
2. Option 2
3. Option 3`e[0m
"@
Write-Host $menu
$choice = Read-Host "Enter your choice: "