Skip to content

ConvertTo‑AnsiString

viscalyxbot edited this page Sep 15, 2025 · 1 revision

SYNOPSIS

Converts a string containing ANSI sequences to properly escaped and terminated ANSI sequences.

SYNTAX

ConvertTo-AnsiString [-InputString] <String> [<CommonParameters>]

DESCRIPTION

The ConvertTo-AnsiString command takes a string that may contain ANSI escape sequences (with or without proper escape characters) and ensures that all ANSI sequences are properly escaped and terminated. It adds the necessary escape character ([char]0x1b) and ensures all sequences end with 'm'.

EXAMPLES

EXAMPLE 1

ConvertTo-AnsiString -InputString "[32mTag [1;37;44m{0}[0m[32m was created '{1}'[0m"

This example converts ANSI sequences without escape characters to properly escaped sequences.

EXAMPLE 2

ConvertTo-AnsiString -InputString "[32mTag [1;37;44m{0}[0m[32m was created '{1}'"

This example ensures that unterminated ANSI sequences are properly terminated with a reset sequence.

EXAMPLE 3

ConvertTo-AnsiString -InputString "`e[32mAlready escaped`e[0m"

This example processes a string that already contains properly escaped ANSI sequences.

PARAMETERS

-InputString

The string containing ANSI sequences to be properly escaped and terminated. The string can contain ANSI sequences that may or may not already have escape characters, and may or may not be properly terminated.

Type: String
Parameter Sets: (All)
Aliases:

Required: True
Position: 1
Default value: None
Accept pipeline input: True (ByValue)
Accept wildcard characters: False

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

INPUTS

System.String

OUTPUTS

System.String

NOTES

This function ensures that all ANSI escape sequences in the input string are properly formatted with the correct escape character and termination. It handles both escaped and unescaped sequences, as well as sequences that may be missing the 'm' terminator.

RELATED LINKS

Clone this wiki locally