{"id":2071,"date":"2015-08-12T00:00:00","date_gmt":"2015-08-11T00:00:00","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/when-to-quote-in-powershell\/"},"modified":"2016-07-28T10:47:58","modified_gmt":"2016-07-28T10:47:58","slug":"when-to-quote-in-powershell","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/sysadmin\/powershell\/when-to-quote-in-powershell\/","title":{"rendered":"When to Quote in PowerShell"},"content":{"rendered":"<div class=\"article-content\">\n<p class=\"start\"> \tWhen do you need to use quotes or not in PowerShell? And when you need them, do you need single quotes or double quotes? At first glance, you would think that PowerShell&#8217;s own help page <a href=\"https:\/\/technet.microsoft.com\/en-us\/library\/hh847740.aspx\">about_Quoting_Rules<\/a> would answer both of those, but it does not! It covers part of the topic, primarily <em>string interpolation<\/em> and <em>here strings<\/em> (both of which we&#8217;ll discuss below). In other words, it assumes you need quotes and then explains some of the factors affecting which-single or double-to choose. But it completely ignores the bigger picture. Meanwhile over on StackOverflow you will find <a href=\"http:\/\/stackoverflow.com\/q\/11434388\/115690\">When do I need to use quotes in PowerShell?<\/a> That poses the canonical question yet barely lands a glancing blow on the answer. So, this article is here to bring together the entire topic of quoting in PowerShell in one place for a handy reference source. <\/p>\n<h2>Do You Need Quotes?<\/h2>\n<p> \tLet&#8217;s start with an example. Consider listing the details of a couple files with the <strong>Get-ChildItem<\/strong> cmdlet-I use its <strong>ls<\/strong> alias to minimize the presence of the cmdlet name, focusing just on the elements that we are quoting or not. Which of these is valid PowerShell and lists the details of the two files in question? <\/p>\n<pre class=\"lang:ps theme:powershell-ise\">\"a.txt\", \"b.txt\" | ls\t# A\na.txt, b.txt | ls\t# B\nls \"a.txt\", \"b.txt\"\t# C\nls a.txt, b.txt\t        # D<\/pre>\n<p> \tThe answer is (A), (C), and (D); assuming the appropriate files exist, all of those return the exact same result (they will give different error messages if the files do not exist). Because both cases (C) and (D) work correctly, you can infer that when the cmdlet is given first, you do not need quotes but you may use them. So let&#8217;s focus on the two primordial cases: case (A) where quotes are needed and case (D) where quotes are not needed: <\/p>\n<pre class=\"lang:ps theme:powershell-ise\">\"a.txt\", \"b.txt\" | ls\t# A\nls a.txt, b.txt\t        # D<\/pre>\n<p> \tTo understand the difference requires an awareness of PowerShell&#8217;s two parsing modes, <em>command parsing<\/em> and <em>execution parsing<\/em>. These two modes work together to allow PowerShell as a <em>scripting<\/em> language to peacefully co-exist with PowerShell as a <em>shell<\/em> language allowing you, the user, to be able to move code between a script and the command line and have it just work. That is, in a script (program) having to type string literals with quotation marks like this&#8230; <\/p>\n<pre class=\"lang:ps theme:powershell-ise\">rm \"a.txt\", \"b.txt\", \"c.txt\"<\/pre>\n<p> \tis quite reasonable. However, at the command line that would be annoying to some, intolerable to others. You should be able to just type this: <\/p>\n<pre class=\"lang:ps theme:powershell-ise\">rm a.txt, b.txt, c.txt<\/pre>\n<p> \tFurthermore, like any good shell, you must be able to type expressions like <strong>5 + 2<\/strong> and get a response of <strong>7<\/strong>, or type <strong>&#8220;hello world&#8221;<\/strong> and get <strong>hello world<\/strong> echoed back. Yet when you type a cmdlet name, e.g. <strong>Get-Process<\/strong>, it should execute the command, not just echo the characters <strong>&#8220;<\/strong><strong>Get-Process<\/strong><strong>&#8220;<\/strong>. Simple, you say, because omitting the quotes signals that it is a command to execute, right? But what if you want to execute a command with a space in it, perhaps <strong>C:\\Program Files\\<\/strong><strong>xyz.exe<\/strong> ? Well, without quotes PowerShell would just try to execute <strong>C:\\Program<\/strong> with <strong>Files\\xyz.exe<\/strong> as its first argument. So clearly it must have quotes because of the space, but if you try &#8220;<strong>C:\\Program Files\\xyz.exe<\/strong>&#8221; it just gets echoed back. Whew! <\/p>\n<p> \tPowerShell&#8217;s two parsing modes allow you to untangle and manage your commands and your scripts with ease. Per Keith Hill&#8217;s reference article, <a href=\"https:\/\/rkeithhill.wordpress.com\/2007\/11\/24\/effective-powershell-item-10-understanding-powershell-parsing-modes\/\">Understanding PowerShell Parsing Modes<\/a>: <\/p>\n<p class=\"note\"> \t&#8220;First [you] need to parse like a traditional shell where strings (filenames, dir names, process names, etc.) do not need to be quoted. Second [you] need to be able to parse like a traditional language where strings are quoted and expressions feel like those you would find in a programming language. In PowerShell, the former is called Command parsing mode and the latter is called Expression parsing mode. It is important to understand which mode you are in and more importantly, how you can manipulate the parsing mode.&#8221; <\/p>\n<p> \tThe rules for selecting a mode (or determining which you are in) are simple: <\/p>\n<ul>\n<li>Start with a letter, underscore, ampersand, dot or backslash =&gt; command parsing mode<\/li>\n<li>Start with anything else =&gt; expression parsing mode<\/li>\n<\/ul>\n<p> \tNote that any leading whitespace in the line (or more precisely, in the statement) does <em>not<\/em> count in determining the start, so &#8220;start&#8221; above really means the first non-whitespace character in the statement. <\/p>\n<p> \tExamples: <\/p>\n<table>\n<thead>\n<tr>\n<td>#<\/td>\n<td>Text<\/td>\n<td>Result<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>Get-ChildItem &#8230;<\/td>\n<td>letter =&gt; cmd mode =&gt; run Get-ChildItem<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>5 + 2<\/td>\n<td>number =&gt; exp mode =&gt; evaluate the expression 5 + 2<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>&#8220;hello world&#8221;<\/td>\n<td>quote =&gt; exp mode =&gt; evaluate, i.e. echo the string<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>&#8220;C:\\Program Files\\xyz.exe&#8221;<\/td>\n<td>quote =&gt; exp mode =&gt; evaluate, i.e. echo the string &#8211; aargh!<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>&amp; &#8220;C:\\Program Files\\xyz.exe&#8221;<\/td>\n<td>ampersand =&gt; cmd mode =&gt; run C:\\Program Files\\xyz.exe &#8211; huzzah!<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>C:\\Program` Files\\xyz.exe<\/td>\n<td>Letter =&gt; cmd mode =&gt; run &#8220;C:\\Program Files\\xyz.exe&#8221; &#8211; again huzzah!<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p> \tThe last three lines above show the problem of spaces within an executable path and how to address the issue: either (row 5) start the line with the call operator ( &amp; ) and quote the string, or (row 6) escape any spaces within the string by preceding each with a backtick (`).Escaping a space makes it act like any other character in the string so then you do not need to surround the string with quotes! <\/p>\n<p> \tYou can also freely intermix command parsing with expression parsing as needed. Consider this short line: <\/p>\n<pre class=\"lang:ps theme:powershell-ise\">if (!(Get-ToolsLoaded)) { return }\n<\/pre>\n<p> \tIt starts in command parsing and PowerShell recognizes a conditional (if) statement. Parentheses start the mode determination process over, so inside the parentheses the exclamation mark forces expression parsing, negating something, that something again starting with parentheses, and again restarting mode determination. Inside those parentheses the letter &#8220;G&#8221; signals command parsing, so <strong>Get-<\/strong><strong>ToolsLoaded<\/strong> is executed, its result negated, then the conditional proceeds or not. Here&#8217;s one more example-this is a real line of code I pulled out of one of my open-source scripts-that shows multiply-nested expressions with different parsing modes (&#8220;exp&#8221; for expression mode parsing; &#8220;cmd&#8221; for command mode parsing): <\/p>\n<p class=\"illustration\"> \t<img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/2264-1-e224a89f-266d-44b3-9e88-a735a8a27ec4.png\" alt=\"2264-1-e224a89f-266d-44b3-9e88-a735a8a27\" \/><\/p>\n<h2>Do You Need to Quote Arguments to a Command?<\/h2>\n<p> \tWith an awareness of parsing modes, now go back to the two equivalent ways to fetch file details: <\/p>\n<pre class=\"lang:ps theme:powershell-ise\">\"a.txt\", \"b.txt\" | ls\t# A\nls a.txt, b.txt\t        # D<\/pre>\n<p> \tApplying the parsing mode rules, case (A) starts in expression mode. It evaluates <strong>&#8220;a.<\/strong><strong>txt&#8221;, &#8220;b.txt&#8221;<\/strong> as an expression and returns a list; that list is fed into a pipeline as input to ls (<strong>Get-ChildItem<\/strong>). The pipeline is yet another place that restarts parsing determination, so <strong>ls<\/strong> is recognized as a command and executed. In case (D), it starts in command parsing mode and again executes <strong>ls<\/strong>. That&#8217;s great&#8230; <em>but why don&#8217;t the arguments to <\/em><strong><em>ls<\/em><\/strong><em> need quotes?<\/em> The answer can be gleaned from the <a href=\"https:\/\/www.microsoft.com\/en-us\/download\/details.aspx?id=36389\">PowerShell Language Specification Version 3.0<\/a>, section 8.2: <\/p>\n<p class=\"note\"> \t&#8220;A command invocation consists of the command&#8217;s name followed by zero or more arguments. The rules governing arguments are as follows: &#8230; An argument that is not an expression, but which contains arbitrary text without unescaped white space, <em>is treated as though it were double quoted<\/em>.&#8221; [emphasis added] <\/p>\n<p> \tThe phrase &#8220;text without unescaped white space&#8221; is rather convoluted and I claim, a bit misleading. What that is saying is: <\/p>\n<ol>\n<li>Text without whitespace is considered quoted, i.e. a string literal like <strong>abc<\/strong>.<\/li>\n<li>Text with escaped whitespace is also considered a string literal, like <strong>two` words<\/strong>.<\/li>\n<li>And incidentally, if there is unescaped whitespace (like <strong>two words<\/strong>) the whitespace is considered a boundary between arguments, so apply (1) and (2) to each argument separately: both <strong>two<\/strong> and <strong>words&#160;<\/strong><strong><\/strong>are considered&#160;<strong><\/strong>string&#160;<strong><\/strong>literals by (1).<\/li>\n<\/ol>\n<p> \tThe definition of <em>expression<\/em> is detailed in section 7 of the specification but the rules are similar to parsing mode determination; suffice to say that <strong>a.txt<\/strong> is <em>not<\/em> an expression, so it is as if you typed &#8220;a.txt&#8221;. <\/p>\n<p> \tOf course, if one of your file names contains spaces, then you must explicitly include the quotes. It is unreasonable to expect PowerShell to be able to figure out that &#8220;a file.text&#8221; is all one name here: <\/p>\n<pre class=\"lang:ps theme:powershell-ise\">ls a file.txt, b.txt<\/pre>\n<p> \tSo you must do this: <\/p>\n<pre class=\"lang:ps theme:powershell-ise\">ls \"a file.txt\", b.txt<\/pre>\n<p> \tAgain, if you prefer to avoid quotes, you always have the option of escaping any embedded whitespace by preceding each space with a backtick: <\/p>\n<pre class=\"lang:ps theme:powershell-ise\">ls a` file.txt, b.txt<\/pre>\n<h2>Do You Need to Quote the Target of an Assignment Statement?<\/h2>\n<p> \tGiven the two assignment statements shown, which is legitimate? <\/p>\n<pre class=\"lang:ps theme:powershell-ise\">$item = 'hello' # A\n$item = hello   # B\n<\/pre>\n<p> \tPer <a href=\"https:\/\/www.microsoft.com\/en-us\/download\/details.aspx?id=36389\">PowerShell Language Specification Version 3.0<\/a>, section 7.11, assignment statement syntax is: <\/p>\n<p> &#160;&#160;&#160;&#160;<em>expression &#160;&#160;operator &#160;&#160;statement<\/em> <\/p>\n<p> \tThe operator in this case is the equals sign (=). The right-hand side of the equals is thus the <em>statement<\/em> component. Typically you think of a statement as starting at the beginning of a line but this is an example to the contrary; statements may occur even within a line! Beginning a statement implies, as you have seen, beginning of parsing determination. In (A) the statement begins with a quote, so it is considered expression mode and the literal <strong>hello<\/strong> is recognized. In (B), however, the first character of the statement is a letter, meaning command mode applies, and it tries to execute <strong>hello<\/strong>, which is not what you want. <\/p>\n<p> \tThus, in assignments, quotes are required. <\/p>\n<h2>Do You Need to Quote Components of an Expression?<\/h2>\n<p> \tGiven the following expressions, which are legitimate? <\/p>\n<pre class=\"lang:ps theme:powershell-ise\">'aaa' + 'foo'\t# A\n'aaa' -eq 'foo'\t# B\n'aaa' + foo\t# C\n'aaa' -eq foo\t# D\n<\/pre>\n<p> \tEach line begins with a quoted string so when parsing determination ensues, all of these lines begin in expression parsing mode. The real question here is what happens after the operator, be it an arithmetic operator (A and C) or a comparison operator (B and D)-are quotes required? The answer here is straightforward if you think about it: you start in expression mode when encountering the left-hand argument, continue and encounter the operator, then continue and encounter the right-hand argument. Nothing in there restarts parsing determination so the right-hand argument must still be a legitimate expression, which means if you want a string literal you have to dress it up as such-with quotes-so A and B are valid. (If, on the other hand, you wanted to execute a function named <strong>foo<\/strong>, you then need to dress it up as such-in expression&#8217;s clothing, as it were-by putting it in a subexpression where parsing determination can restart, i.e. : <strong>&#8216;<\/strong><strong>aaa<\/strong><strong>&#8216; + (foo)<\/strong>. <\/p>\n<h2>Which Quotes to Use?<\/h2>\n<p> \tYou have determined that you need quotes; the next determination is whether you need single quotes (&#8216;) or double quotes (&#8220;). The rule for this is very simple to state but needs a bit of explanation, which follows: use single quotes for literal (or static) strings and double quotes for interpolated (or interpreted) strings. <\/p>\n<p> \tLet&#8217;s explore this in detail. For this first example I will borrow from PowerShell&#8217;s help page, <a href=\"https:\/\/technet.microsoft.com\/en-us\/library\/hh847740.aspx\">about_Quoting_Rules<\/a>, reformatting a bit to add clarity. Assume you have defined the variable <strong>$<\/strong><strong>i<\/strong> to be 5 before executing any of these: <\/p>\n<table>\n<thead>\n<tr>\n<td>#<\/td>\n<td>Text<\/td>\n<td>Result<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1a<\/td>\n<td>&#8220;The value of $i is $i.&#8221;<\/td>\n<td>The value of 5 is 5.<\/td>\n<\/tr>\n<tr>\n<td>1b<\/td>\n<td>&#8216;The value of $i is $i.&#8217;<\/td>\n<td>The value of $i is $i.<\/td>\n<\/tr>\n<tr>\n<td>1c<\/td>\n<td>&#8220;The value of `$i is $i.&#8221;<\/td>\n<td>The value of $i is 5.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p> \tIn this first group (1a) shows the effect of a double-quoted string. Any embedded variables are interpolated (or interpreted if you prefer) so the output is different from the input. The (1b) row, on the other hand, shows that with single quotes the string is completely static: the output is identical to the input and no interpolation occurs. The (1c) row shows that you can actually mix interpolation with literals: precede any dollar sign with a backtick to prevent it from initiating an interpolation for that one instance. <\/p>\n<table>\n<thead>\n<tr>\n<td>#<\/td>\n<td>Text<\/td>\n<td>Result<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>2a<\/td>\n<td>&#8220;The value of $(2+3) is $i.&#8221;<\/td>\n<td>The value of 5 is 5.<\/td>\n<\/tr>\n<tr>\n<td>2b<\/td>\n<td>&#8216;The value of $(2+3) is $i.&#8217;<\/td>\n<td>The value of $(2+3) is $i.<\/td>\n<\/tr>\n<tr>\n<td>2c<\/td>\n<td>&#8220;The value of `$(2+3) is $i.&#8221;<\/td>\n<td>The value of $(2+3) is 5.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p> \tThis second group shows the analogous expressions but this time using a <strong>$(&#8230;)<\/strong> subexpression. Within a double quoted string, anything inside the subexpression is evaluated, showing that you can have much more than just a simple variable name interpolated. The above example is contrived of course just to keep the subexpression terse. A common need for such a subexpression, though, comes when you want to interpolate the property of an object. Say you have a <strong>$process<\/strong> object (<code>$process = get-process -name winword<\/code>) and you want to embed the company name associated with the process in a string. <\/p>\n<table>\n<thead>\n<tr>\n<td>#<\/td>\n<td>Text<\/td>\n<td>Result<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>3a<\/td>\n<td>&#8220;Word by $process.Company&#8221;<\/td>\n<td>Word by System.Diagnostics.Process (WINWORD).Company<\/td>\n<\/tr>\n<tr>\n<td>3b<\/td>\n<td>&#8220;Word by $($process.Company)&#8221;<\/td>\n<td>Word by Microsoft Corporation<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p> \tEverybody tries approach (3a) first-but do not try it with your boss looking on or you will have one of those awkward moments.:-) String interpolation starts at the dollar sign and stops at the first character that cannot be part of a simple variable name, in this case the period. So <strong>$process<\/strong> alone is interpolated and, being a complex object, you get its type name and the name of the process. Use a subexpression to get what you really want, as in (b). <\/p>\n<p> \tBesides variable interpolation, double-quoted strings also include <em>special character interpretation<\/em>. That is if you type <strong>`t<\/strong> inside a double-quoted string it will output a tab. Within a single-quoted string, special characters are not interpreted, but output just as you typed them. See <a href=\"https:\/\/technet.microsoft.com\/en-us\/library\/hh847835.aspx\">about_Special_Characters<\/a> for other such special characters. I have highlighted the <strong>`t<\/strong> here just for clarity: <\/p>\n<table>\n<thead>\n<tr>\n<td>#<\/td>\n<td>Text<\/td>\n<td>Result<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>4a<\/td>\n<td>&#8220;Item1`tItem2`tItem3&#8221;<\/td>\n<td>Item1&#160;&#160;&#160;&#160;&#160;Item2&#160;&#160;&#160;&#160;&#160;Item3<\/td>\n<\/tr>\n<tr>\n<td>4b<\/td>\n<td>&#8216;Item1`tItem2`tItem3&#8217;<\/td>\n<td>Item1`tItem2`tItem3<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Including Quotes within Quotes<\/h2>\n<p> \tSometimes you just need to put literal quotes within a string literal. The table below shows all the combinations and how you need to manipulate the input to get the desired output. In (5a) and (5b) you can see that including the <em>other<\/em> quote-the one you are not using to delimit the string-requires no special action. (6a) and (6b) show how to put double quotes within double quotes. You have two choices: either repeat the double quote character (6a) or escape the double quote character (6b) with a backtick. Finally putting single quotes within single quotes allows only one option, doubling the single quote (7a). You cannot escape with a backtick (7b) because inside a single quoted string everything is a literal-including a backtick-so that single quote in the middle ends the string prematurely! <\/p>\n<table>\n<thead>\n<tr>\n<td>#<\/td>\n<td>Text<\/td>\n<td>Result<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>5a<\/td>\n<td>&#8220;Single quote &#8216; within double quotes&#8221;<\/td>\n<td>Single quote &#8216; within double quotes<\/td>\n<\/tr>\n<tr>\n<td>5b<\/td>\n<td>&#8216;Double quote &#8221; within single quotes&#8217;<\/td>\n<td>Double quote &#8221; within single quotes<\/td>\n<\/tr>\n<tr>\n<td>6a<\/td>\n<td>&#8220;Double quote &#8220;&#8221; within double quotes&#8221;<\/td>\n<td>Double quote &#8221; within double quotes<\/td>\n<\/tr>\n<tr>\n<td>6b<\/td>\n<td>&#8220;Double quote `&#8221; within double quotes&#8221;<\/td>\n<td>Double quote &#8221; within double quotes<\/td>\n<\/tr>\n<tr>\n<td>7a<\/td>\n<td>&#8216;Single quote &#8221; within single quotes&#8217;<\/td>\n<td>Single quote &#8216; within single quotes<\/td>\n<\/tr>\n<tr>\n<td>7b<\/td>\n<td>&#8216;Single quote `&#8217; within single quotes&#8217;<\/td>\n<td>error<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p> \tA special note on (7a): It looks like there is a double quote in the middle that somehow, magically, becomes a single quote in the output. But not so! That is simply two single quotes juxtaposed. Thus, the technique is the same for single-quoted or double-quoted strings: double the same quote character to get a single one in the output. <\/p>\n<h2>Multi-Line Strings<\/h2>\n<p> \tPowerShell lets you create strings spanning multiple lines (as a variety of scripting languages do) using a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Here_document\">here string<\/a>. A <em>here string<\/em>, or <em>here document<\/em>, is simply a piece of text embedded within your program that is treated as if it came from a separate file; in other words, it&#8217;s right <em>here<\/em>. PowerShell&#8217;s here strings can be created with single quotes or double quotes and behave as you should now expect: single-quoted they are pure literals; double-quoted allows for interpolation of variables, subexpressions, and special characters. <\/p>\n<p> \tA here string must be formatted thusly: <\/p>\n<pre class=\"lang:ps theme:powershell-ise\">@\"&lt;ENTER&gt;\nAny text on any number of lines...&lt;ENTER&gt;\n\"@<\/pre>\n<p> \tThat is, nothing but whitespace and  may follow the opening token (@&#8221;). And most important, the closing token must be absolutely the first character on the line-no preceding whitespace allowed! The value of the here string is everything between the two demarcated <em>&lt;ENTER&gt;<\/em>s shown above. <\/p>\n<p> \tYou can actually embed line breaks in a regular string just as simply as a here string, but a here string has one main advantage:  you can freely embed quotes just like any other character (8a, 8c), no doubling or escaping necessary!  <\/p>\n<table>\n<thead>\n<tr>\n<td>#<\/td>\n<td>Text<\/td>\n<td>Result<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>8a<\/td>\n<td>@&#8221;<br \/>Word<br \/>by  &#8220;$process.Company&#8221;<br \/>&#8220;@<\/td>\n<td>Word<br \/>by &#8220;System.Diagnostics.Process (WINWORD).Company&#8221;<\/td>\n<\/tr>\n<tr>\n<td>8b<\/td>\n<td>@&#8221;<br \/>Word<br \/>by $($process.Company)<br \/>&#8220;@<\/td>\n<td>Word<br \/>by Microsoft Corporation<\/td>\n<\/tr>\n<tr>\n<td>8c<\/td>\n<td>@&#8217;<br \/>Word<br \/>by  &#8216;$process.Company&#8217;<br \/>&#8216;@<\/td>\n<td>Word<br \/>by  &#8216;$process.Company&#8217;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>A Word About Format Strings<\/h2>\n<p> \tFor completeness, it is worth mentioning the standard .NET format string. In C#, you can, for example do something like this&#8230; <\/p>\n<pre class=\"lang:ps theme:powershell-ise\">Console.WriteLine(\"{0}, your balance is {1}. (Status {2})\", name, balance, status);<\/pre>\n<p> \t&#8230;which might print something like this: <\/p>\n<pre class=\"lang:ps theme:powershell-ise\">Mr. Smith, your balance is $100. (Status OK)<\/pre>\n<p> \tWhat <strong>Console.WriteLine<\/strong> is effectively doing is using the <strong>String.Format<\/strong> method, which takes a format string containing place holders followed by a list of objects to populate those placeholders. You can use <strong>String.Format<\/strong> in PowerShell with the rather terse <strong>-f<\/strong> operator. This is the PowerShell equivalent: <\/p>\n<pre class=\"lang:ps theme:powershell-ise\">\"{0}, your balance is {1}. (Status {2})\" -f $name, $balance, $status<\/pre>\n<p> \tEffectively that is doing exactly the same as this: <\/p>\n<pre class=\"lang:ps theme:powershell-ise\">\"$name, your balance is $balance. (Status $status)\"<\/pre>\n<p> \t&#8230;so you might jump to the conclusion that you <em>must<\/em> use a double-quoted string when using the -f (<strong>String.Format<\/strong>) operator. But no! In this instance either single quotes or double quotes will do: <\/p>\n<table>\n<thead>\n<tr>\n<td>#<\/td>\n<td>Text<\/td>\n<td>Result<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>9a<\/td>\n<td>&#8220;{0}, your balance is {1}. (Status {2})&#8221; -f $name, $balance, $status<\/td>\n<td>Welcome Mr. Smith, your balance is $100. (Status OK)<\/td>\n<\/tr>\n<tr>\n<td>9b<\/td>\n<td>&#8216;{0}, your balance is {1}. (Status {2})&#8217; -f $name, $balance, $status<\/td>\n<td>Welcome Mr. Smith, your balance is $100. (Status OK)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p> \tWhy is the single-quoted string in this peculiar case acting like active code instead of just a string literal? The answer is that it is <em>not<\/em>; it really is just a string literal, character for character. Those placeholders are plain text, as far as the string is concerned; it is the <strong>String.Format<\/strong> method that reads that string and treats those placeholders differently. <\/p>\n<h2>Summary<\/h2>\n<p> \tStrings are such a vital concept in any language that it is important to have a good grounding in their fundamentals. PowerShell being a scripting language tends to have more flexibility surrounding their use, covering a wide range of scenarios. In this article I have brought together all of that in one place to make it easy to learn everything you need, and to have a single reference source to come back to when you have questions about quoting strings. So go back and look at your own code; chances are you will find you can remove a lot of clutter by removing many unnecessary quotes! <\/p>\n<div class=\"supernote\"> <a href=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/2264-quotingWallchart.PNG\"><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/2264-sorenspdf-350x220.jpg\" alt=\"2264-sorenspdf-350x220.jpg\" \/><\/a> <\/p>\n<p>A reference wallchart of the information in this article is available to download and print <a href=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/2264-PS%20Quoting%20Wallchart_1_0_1.pdf\">here<\/a><\/p>\n<\/p><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>The one question about PowerShell that trips up almost everyone is about when and how to quote strings. Because PowerShell replaces the old command shell, it has to be able to work the way that it did with string parameters, but it also has to behave like a .NET scripting language to replace VBA. PowerShell grapples with this apparent contradiction, and manages to square the circle. Michael Sorens explains the how and when of PowerShell quoting.&hellip;<\/p>\n","protected":false},"author":221868,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[35],"tags":[4143,4635,4871],"coauthors":[],"class_list":["post-2071","post","type-post","status-publish","format-standard","hentry","category-powershell","tag-net","tag-powershell","tag-sysadmin"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2071","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/221868"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=2071"}],"version-history":[{"count":4,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2071\/revisions"}],"predecessor-version":[{"id":41212,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2071\/revisions\/41212"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=2071"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=2071"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=2071"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=2071"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}