PowerShell – Double Quotes vs Single Quotes

Ever wanted to know what the effect will be when you use ‘single quotes’ versus “double quotes” in Windows PowerShell?

Let’s start by setting a variable, $i to 5.

PS> $i = 5

 

Using Double Quotes:
PS> write-host “The value of $i is $i”
The value of 5 is 5

This results in the value of $i being expanded so the value of 5 is returned.

 

Using Single Quotes followed by the Unquoted Variable $i
PS> write-host ‘The value of $i is $i’
The value of $i is $i

This results in the variable $i being returned exactly as you typed it, so we get $i and not the value 5.

 

Using Single Quotes followed by the Unquoted Variable $i
PS> write-host ‘The value of $i is’ $i
The value of $i is 5

This results in the variable $i being returned exactly as you typed it. To return the value 5 we simply type $i without any quotes.

 

 
Comments

No comments yet.

Leave a Reply