This article describes how to prevent a character in a string from being escaped or a partial character being treated as a variable in a PowerShell string, so that all characters in the string are exported as they are.
In PowerShell, an escape character is a special character that begins with a character (·) and can be used to implement functions such as wrapping, tab, and so on. The variable begins with a symbol ($) and represents a value. We have introduced the escape character in the previous article, and we have also described the inclusion of variables in the string. Interested friends can go and understand.
You can use a pair of double quotes in a PowerShell to cause a string, or a pair of single quotes to cause a string. One of the biggest differences between single and double quotes is that double quotes cause a string to be called an extensible string, whereas single quotes result in strings called literal strings. A string that is enclosed in single quotes, whether the escape character is present or the variable is present, is ignored, and everything is shown literally.
Copy Code code as follows:
PS c:\users\spaybow> $p = "PowerShell"
PS c:\users\spaybow> $str = "Hello ' t$p"
PS c:\users\spaybow> $str 1= ' Hello ' t$p '
PS c:\users\spaybow> $str
Hello PowerShell
PS c:\users\spaybow> $str 1
Hello ' t$p
The example above clearly shows the difference between single and double quotes. ' t is an escape string that represents the TAB key, $p is a string, and the value is PowerShell.
When you use double quotes, the result of the $str is "Hello PowerShell", and when you use single quotes, the output is "Hello ' t$p."
By using single quotes to assign a value to a string, we can prevent these uncertainties, such as escaping characters or variables, from being included in the string, resulting in a fixed string.
About the PowerShell in the forbidden string contains escape characters or variables, this article on the introduction of so many, I hope to help you, thank you!