{"id":5441,"date":"2015-06-20T00:01:00","date_gmt":"2015-06-20T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/06\/20\/weekend-scripter-understanding-quotation-marks-in-powershell\/"},"modified":"2019-02-18T09:47:21","modified_gmt":"2019-02-18T16:47:21","slug":"weekend-scripter-understanding-quotation-marks-in-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-understanding-quotation-marks-in-powershell\/","title":{"rendered":"Weekend Scripter: Understanding Quotation Marks in PowerShell"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Guest blogger, Don Walker, explores using single and double quotation marks in Windows PowerShell.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Welcome new guest blogger, Don Walker. Don is a coworker of guest blogger, Tim Bolton, and he worked with Tim on a project that Tim blogged about on June 18, 2015, <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/using-powershell-grid-view-for-user-info\/\" target=\"_blank\">Using PowerShell Grid View for User Info<\/a>.<\/p>\n<p>Here&#039;s Don&#039;s contact information:<\/p>\n<p style=\"margin-left:30px\"><b>Twitter<\/b>:&nbsp;@EngineeringDon<br \/> <b>LinkedIn<\/b>: www.linkedin.com\/in\/EngineeringDon<\/p>\n<p>A while back, I was asked to explain the difference between using single quotation marks ( <b>&lsquo;<\/b> ) and double quotation marks <br \/>( <b>&ldquo;<\/b> ) to define a string value, and when to use each. The feedback I received encouraged me to turn my explanation into an article so I could share it with others. My first thought was, &ldquo;I don&rsquo;t need to do that. How to define strings is too simple a topic.&rdquo;<\/p>\n<p>But thinking more about it, it is worth sharing. Understanding the difference between strings defined by single quotation marks or double quotation marks can save a huge amount of time and frustration. The two methods may initially look exactly the same, but the underlying action that occurs is very different.<\/p>\n<p>Windows PowerShell can&#039;t recognize a string value unless delimiters (or boundaries) are defined by the user. In Windows PowerShell, single and double quotation marks are used to define the string. Essentially, &ldquo;here is my start&rdquo; and &ldquo;here is my end.&rdquo;<\/p>\n<p>Here is an example of strings being defined and set to variables:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/wes-6-20-15-1.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/wes-6-20-15-1.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>Notice that in the last example, the string is not contained in quotation marks so Windows PowerShell mistakenly confuses it for a command instead of a string value.<\/p>\n<h3>Single quotation marks define a literal string<\/h3>\n<p>Literal strings treat each character as a fixed value. What you see is what you get. Here is an example of how a string with single quotation marks ignores the variable character in Windows PowerShell ( <b>$<\/b> ) and treats it as a regular dollar sign:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/wes-6-20-15-2.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/wes-6-20-15-2.png\" alt=\"Image of command\" title=\"Image of command\" \/><\/a><\/p>\n<p><span style=\"font-size:12px\">Using single quotation marks for basic strings is a best practice because there are no unexpected surprises with literal values. The only surprise that I have found has been attempting to use a single quotation mark character within a string that uses single quotation marks.&nbsp;<\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/wes-6-20-15-3.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/wes-6-20-15-3.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>Windows PowerShell has a built-in remedy for this. Place an additional single quotation mark immediately after the first instance, for example:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/wes-6-20-15-4.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/wes-6-20-15-4.png\" alt=\"Image of command\" title=\"Image of command\" \/><\/a><\/p>\n<p>Windows PowerShell also gives you the ability to define literal strings and concatenate them with variable values to form more complex strings, for example:<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/6232.A.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/6232.A.PNG\" alt=\"Image of command\" title=\"Image of command\" \/><\/a><\/p>\n<h3>Double quotation marks define more dynamic parsing string<\/h3>\n<p>Parsing strings analyze each character within and parse certain characters with a special action, such as recognizing a variable and retrieving its value. This allows placing string values within a delimited string. Here is an example of using double quotation marks in a string to parse the value of a variable ($foo = &lsquo;bar&rsquo;) and place it within the string&rsquo;s value: <br \/><span style=\"font-size:12px\"><\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/wes-6-20-15-6.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/wes-6-20-15-6.png\" alt=\"Image of command\" title=\"Image of command\" \/><\/a><\/p>\n<p><span style=\"font-size:12px\">This is the most common way to use double quotation marks in a string. As you get into loops and functions within your scripting, injecting changing variable values into static string output will keep your script&rsquo;s output consistent. A real-world example of this could be a script that targets multiple servers and returns a string value if a certain process is running:<\/span><\/p>\n<p style=\"margin-left:30px\"><span style=\"font-size:12px\">&ldquo;${TargetServer} has ${process} running&rdquo;<\/span><\/p>\n<p>In addition to parsing variables, strings defined within double quotation marks also parse the backtick character ( <b>`<\/b> ) to define escape characters and literal character values. Escape characters allow special actions to occur when processing the string, such as inserting a Tab space or starting a new line.<\/p>\n<p>If a backtick is used, but it does not call an escape character, the character immediately after it will be a literal value and ignore any parsing rules. I recommend being familiar with the following backtick usage:<\/p>\n<ul>\n<li><b>`n<\/b> &ndash; insert new line<\/li>\n<li><b>`t<\/b> &ndash; insert horizontal tab<\/li>\n<li><b>`$<\/b> &ndash; insert dollar sign instead of parsing a variable value<\/li>\n<li><b>`&rdquo; <\/b>&ndash; insert double quotation mark character instead of ending string<\/li>\n<li><b>&#8220;<\/b> &ndash; insert a single backtick character<\/li>\n<\/ul>\n<p>Here is an example of the backtick being used as an escape character:<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/2273.B.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/2273.B.PNG\" alt=\"Image of command\" title=\"Image of command\" \/><\/a><\/p>\n<p>It is up to you as the Windows PowerShell user to decide which quotation mark style works best for your situation, but here are my final thoughts on the subject:<\/p>\n<ul>\n<li>Single quotation marks ( <b>&lsquo;<\/b> ) define a literal string. What you see is what you get.<\/li>\n<li>Strings that use single quotation marks are the preferred method.<\/li>\n<li>Double quotation marks ( &ldquo; ) define a dynamic, parsing string.<\/li>\n<li>Strings that use double quotation marks are most commonly used to invoke expressions or remote commands within a script.<\/li>\n<li>Strings begin and end with the same type of quotation mark (single or double).<\/li>\n<li>You should familiarize yourself with escape characters, if you haven&rsquo;t already.<\/li>\n<\/ul>\n<p>~Don<\/p>\n<p>Thanks Don. This is a very helpful post.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Guest blogger, Don Walker, explores using single and double quotation marks in Windows PowerShell. Microsoft Scripting Guy, Ed Wilson, is here. Welcome new guest blogger, Don Walker. Don is a coworker of guest blogger, Tim Bolton, and he worked with Tim on a project that Tim blogged about on June 18, 2015, Using PowerShell [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[596,589,51,56,3,61,45],"class_list":["post-5441","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-don-walker","tag-fundamentals","tag-getting-started","tag-guest-blogger","tag-scripting-guy","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Guest blogger, Don Walker, explores using single and double quotation marks in Windows PowerShell. Microsoft Scripting Guy, Ed Wilson, is here. Welcome new guest blogger, Don Walker. Don is a coworker of guest blogger, Tim Bolton, and he worked with Tim on a project that Tim blogged about on June 18, 2015, Using PowerShell [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/5441","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=5441"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/5441\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=5441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=5441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=5441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}