Wednesday, April 11, 2012

PowerShell Basics Part-1: Single Quotes (') and Double Quotes ("") in PowerShell.



When new users start working on PowerShell the the main problem which they have faced is basic of PowerShell or we can say basic of any scripting language.

In this "PowerShell basics" tutorial series we are planning to cover as much as basics of PowerShell we can. These basics are not in any particular manner. The things which come in my mind and start covering them.

In the Part-1 we are covering "Single Quotes (') and Double Quotes ("").

In PowerShell we can use Single quotes and double quotes to  access the folder and files which have space in there names. Without "single or Double quotes" PowerShell consider blank space as separator.

Have a Look.

Try to Get-Childitem of "Program files" with out space, and you will get an error that it not able to find "C:\Program". As i mentioned before that without quotes PowerShell treat "blank space " as separator.

11-04-2012 14-44-25

Now lets wrap the the "Program Files" in Double or Single Quote. And this time it works Perfectly.

11-04-2012 14-44-50

we can also use the single quotes to achieve the same. Let see another example.

I have a text file named as " Space test.txt", lets try to open it without quotes first.

11-04-2012 14-57-24

OOps !! same error..

11-04-2012 14-58-49

Wrap it Single or Double Quotes,  this time lets wrap it in a Single quote and see what will happen.  Working :)

11-04-2012 15-00-28

But..the main difference in Single and Double quotes are described below.

"Double Quotes"

The Main Difference between "single and Double quotes" are that Variable are expands in "Double Quotes" but they don't expand in single quotes.

$name = "Aman"

11-04-2012 15-36-47

In variable $name i am assigning a value "Aman" let see if variable expand in $name.

You can see that in  Write-Host "My Name is $name" , $name is changed with the value "Aman"

11-04-2012 15-38-04

'Single Quotes'

The Single Quotes mainly used to mention the path to files and folders. I already mentioned the main difference above between "single and double" quotes that variables don't get expand in Single Quotes.

Let's look at same above example but with single quotes.

$name = "Aman"

You can see that in Write-Host "My Name is $name" , $name is not changed, that remain constant.

11-04-2012 15-46-16

Thanks for Reading.

Aman Dhally

No comments:

Post a Comment