Differences between powershell single double quotation mark strings

Source: Internet
Author: User

Recently, I have been doing some script automation and found a very interesting thing, as shown below. If the user account contains the $ symbol, an exception "user cannot be found" will be thrown.

New-spsite $ communityurl-owneralias "asiapacific '\ $ exsquaredev001"-name "$ communityname"-template "STS #0"-contentdatabase $ communitydatabase
New-spsite: user cannot be found.

I searched on msdn and found an articleArticleAbout this: http://technet.microsoft.com/en-us/library/ff730965.aspx

Quotation marks in Windows powershell

First, let's review the basic distinction between Single and Double quotation marks in the Windows powershell language. It concerns the interpretation of variables.

    • Double quotes. When a command or expression that contains des variables is enclosed in double quotes ("), the variables are replaced by their values before the command is executed.

    • Single quotes.When a command or expression that contains des variables is enclosed in single quotes ('), the variables are not replaced by their values. Instead, they are interpreted literally.

There's actually a lot more to it-nested quotes and doubled quotes and escaped quotes and quotes in here-strings -- but these are the basics. for the rest, at the windows powershell prompt, type:

    • Get-help about_quoting_rules

Because $ is the symbol of the powershell variable, different quotation marks are processed differently.

The powershell variable in double quotation marks is calculated first and then output.

Powershell variables in single quotes are directly output as strings.

See the following example:

Use double quotation marks to assign values,

Variable assignment: $ exsquareserviceaccount = "asiapacific \ $ exsquaredev001"

Output variable value: asiapacific \

Because the variable is recognized later, and no value is assigned, it is null at this time, and the output is the previous string.

If you use single quotes,

Variable assignment: $ exsquareserviceaccount = 'asiapacpacific \ $ exsquaredev001'

Output variable value: asiapacific \ $ exsquaredev001

The entire string will not be replaced with variables and will be output directly.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.