Difference between single quote (‘) and double quote (“) in PowerShell?

PowerShellMicrosoft TechnologiesSoftware & Coding

There is no such difference between the single quote (‘) and double quote(“) in PowerShell. It is similar to a programming language like Python. We generally use both quotes to print the statements.

Example

PS C:\> Write-Output 'This will be printed using Single quote'
This will be printed using Single quote
PS C:\> Write-Output "This will be printed using double quote"
This will be printed using double quote

But when we evaluate any expression or print variable it makes a clear difference.

$date = Get-Date
Write-Output 'Today date is : $date'
Today date is : $date
Write-Output "Today date is : $date"
Today date is : 11/02/2020 08:13:06

You can see in the above example that a single quote can’t print the variable output and instead it is printing the name of the variable, while the double quotes can print the output of the variable. Even if we try to evaluate a variable name it can’t be done using a single quote.

Example

PS C:\> Write-Output 'Today date is : $($date)'
Today date is : $($date)

Another example of multiplication operation,

PS C:\> Write-Output "Square of 4 : $(4*4)"
Square of 4 : 16
PS C:\> Write-Output 'square of 4 : $(4*4)'
square of 4 : $(4*4)

Take an example of printing multiple statements using an array.

Example

$name = 'Charlie'
$age = 40
$str = @"
New Joinee name is $name
Her age is $age
"@
$str

Output

New Joinee name is Charlie
Her age is 40
He will receive 1200 dollar bonus after 2 years

The above output is printed properly but when we use the single quote, it won’t print the variable name.

Example

$str = @'
New Joinee name is $name
Her age is $age
He will receive $($age*30) dollar bonus after 2 years
'@

Output

New Joinee name is $name
Her age is $age
He will receive $($age*30) dollar bonus after 2 years

It is concluded that, use a single quote only to print the plain text but to print variables and evaluating other expressions in the string, use the double quote in PowerShell.

raja
Published on 09-Nov-2020 06:49:46
Advertisements