Difference between single and double quotes in Powershell

You might have come across the question “What are the differences between single and double quotes in Powershell” when you are working with string manipulation. Both quotes are used to delimit string values. In general, you should use single quotes, unless you are required to replace a string variable with concatenation of two string values (string literal and string variable).

If you put double quotes, inside double quotes, powershell looks for the $ character. If it finds it, it assumes that any following characters up to the next white space are a variable name. It replaces the variable reference with the variable’s contents. Consider the following scenario:

$x = "Hello"
$y = "$x World"

The $y variable will now contain “Hello World” because the double quote concatenates the content of the variable $x with remaining string value. In general, use only single quotes if you don’t have the above need.

Advertisement

Leave a Comment