Category Archives: Powershell-Basics

Quotes in Powershell

In General Quotes are considered to be there just to let interpreter know that the value enclosed within quotes should be treated as a single string. But in reality there are many other uses and types of quotes supported by Powershell. The two types of Quotes are Single Quotes and Double Quotes.

Let’s say we need to pass a value to a parameter which contains special characters, for this purpose you can use any of these it doesn’t make any difference, both will let you do the same thing.

clip_image002[5]

In the above example, I used the quotes to let interpreter treat both words as a single string, a common example with File Paths and both quotes worked.

Back Quote Character ( ` ) aka Back tick

This is usually the upper leftmost key below escape. This comes to rescue the situations where you want to quote a single character.

clip_image004[5]

Variable Expansion

Double Quote has a unique use\feature which is not there with single quote, if the string enclosed in double quotes contains a variable reference starting with a “$”, it will be replaced or expanded by the string representation of the value stored in the variable. It will be more clear by the following example:

clip_image006[5]

As you can see in the example above when we enclosed the variable $a in single quotes the Write-Host Command treated it just as a simple string but when we enclosed the variable $a in double quotes both commands or I should say cmdlets expanded the value which was there in the variable and used it while executing the cmdlet.

How to Suppress Expansion?

Now let’s say we want to suppress expansion at one place for a single variable but not for rest of the variables in the output to achieve this we need to again use the back quote or back tick. Let’s see how to do this.

clip_image008[5]

In the above example see at the first occurrence of $a and $b I specifically used the back tick so that the interpreter doesn’t expand & replace the variable values.

New Line Character ( `n )

In case you want to move a part of output to a new line you can use this special sequence. You can achieve this with a back tick and n. you can place this in either single or double quotes. Please refer the example below:

clip_image010[5]

Horizontal Tab ( `t )

If you want to get a horizontal tab in your output you can use the Horizontal Tab special Sequence. Please refer the example below:

clip_image012[5]

Thanks,
Sarabpreet Singh Anand