{"id":9991,"date":"2006-07-15T14:26:00","date_gmt":"2006-07-15T14:26:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2006\/07\/15\/variable-expansion-in-strings-and-here-strings\/"},"modified":"2019-02-18T13:21:32","modified_gmt":"2019-02-18T20:21:32","slug":"variable-expansion-in-strings-and-here-strings","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/variable-expansion-in-strings-and-here-strings\/","title":{"rendered":"Variable expansion in strings and here-strings"},"content":{"rendered":"<p>PSMDTAG:FAQ: What is the difference between single quoted and double quoted strings?&nbsp; ANSWER:&nbsp; Double quoted string expand variables and single quoted strings do not.<\/p>\n<p>Example:<\/p>\n<p><font face=\"Courier New\" size=\"1\">PS&gt; <\/font><font face=\"Courier New\"><font size=\"1\"><strong>$x=&#8221;TEST&#8221;<br \/><\/strong>PS&gt; <\/font><\/font><font face=\"Courier New\"><font size=\"1\"><strong>&#8220;This is a $x&#8221;<br \/><\/strong>This is a TEST<br \/>PS&gt; <\/font><\/font><font face=\"Courier New\"><font size=\"1\"><strong>&#8216;This is a $x&#8217;<br \/><\/strong>This is a $x<\/font><\/font><\/p>\n<p>&nbsp;<\/p>\n<p>PSMDTAG:FAQ: How do variables expand in strings?<\/p>\n<p>PSMDTAG:FAQ: Why don&#8217;t properties work with variable expansion in strings?<\/p>\n<p>Variables get expanded in strings not property expressions.&nbsp; Here is an example of a property expression that you might like to use that doesn&#8217;t work the way you might think it would:<\/p>\n<p><font face=\"Courier New\" size=\"1\">PS&gt; Calc<br \/>PS&gt; $c = Get-Process Calc<br \/>PS&gt; &#8220;Calc uses <font color=\"#ff0000\">$c<\/font>.Handles Handles&#8221;<br \/>Calc uses <font color=\"#ff0000\">System.Diagnostics.Process (calc)<\/font>.Handles Handles<\/font><\/p>\n<p>In this example, $c gets expanded to &#8220;System.Diagnostics.Process (calc)&#8221;.&nbsp;&nbsp; The problem is that you wanted the $c.Handles to be expanded.&nbsp; <\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>PSMDTAG:FAQ: How do I expand an expression in a string?<\/p>\n<p>Windows PowerShell will expand a variable or an expression in&nbsp; a string.&nbsp;<br \/>Variables look like: &nbsp;&nbsp;&nbsp; &nbsp;$variable<br \/>Expressions look like: &nbsp;$(expression)<\/p>\n<p>Thus to get $c.Handles expanded you do the following:<\/p>\n<p><font face=\"Courier New\" size=\"1\">PS&gt; &#8220;Calc uses <font color=\"#ff0000\">$($c.Handles)<\/font> Handles&#8221;<br \/>Calc uses <font color=\"#ff0000\">42<\/font> Handles<\/font><\/p>\n<p>Now from here, it gets even better.&nbsp; You can put anything put any code you want into that expression.&nbsp; You can put a 40 page script in there if you want.&nbsp; Here is an example:<\/p>\n<p><font face=\"Courier New\" size=\"1\">PS&gt; <strong>cat t.ps1<br \/><\/strong>&#8220;<br \/>Cmds with &gt; 800 handles are: <font color=\"#ff0000\">$(<br \/>&nbsp;&nbsp; $limit = 800<br \/>&nbsp;&nbsp; $procs = Get-Process |where {$_.handles -ge $limit}<br \/>&nbsp;&nbsp; foreach ($p in $procs)<br \/>&nbsp;&nbsp; {&nbsp; &#8216;`n`t{0}&#8217; -f $p.Name.ToUpper()<br \/>&nbsp;&nbsp; }<br \/>)<br \/><\/font>&#8220;<\/p>\n<p>PS&gt; <strong>.\\t.ps1<\/strong><\/font><br \/><font face=\"Courier New\" size=\"1\">Cmds with &gt; 800 handles are:<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CCMEXEC<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CSRSS<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IEXPLORE<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IEXPLORE<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LSASS<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OUTLOOK<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; POWERSHELL<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PS<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SEARCHINDEXER<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SVCHOST<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SYSTEM<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WINLOGON<br \/><\/font><\/p>\n<p>&nbsp;<\/p>\n<p>So by using $(), you can do almost anything you could&nbsp;want to do.&nbsp; <\/p>\n<blockquote>\n<p><em>&#8220;ALMOST &nbsp;?!!!??&#8221;<\/em><\/p>\n<\/blockquote>\n<p>Yeah &#8211; almost but not really everything.&nbsp; Notice that I used single quotes for the format string in the expression:&nbsp; &#8216;`n`t{0}&#8217; .&nbsp; The reason I did that is that if I used double quotes, it would have caused a parser error.&nbsp; I could have used escape characters but that wouldn&#8217;t help me make the point I&#8217;m making so &#8230;&nbsp; <\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>PSMDTAG:FAQ:&nbsp; How do I show double quotes within a double quoted string?&nbsp; ANSWER: escape them with a backtick `&#8221; or use Here-Strings.<\/p>\n<p>A Here-String is a string which starts with a @&#8221; and ends with a &#8220;@ (on a line by itself).&nbsp; Here-Strings can use any character you want until it sees a &#8220;@ which terminates the string.&nbsp; <\/p>\n<p><font face=\"Courier New\" size=\"1\">PS&gt; <strong>cat t.ps1<br \/><\/strong><font color=\"#ff0000\">@&#8221;<br \/><\/font><font color=\"#ff0000\">&#8220;Cmds&#8221;<\/font> with &gt; 800 handles are: $(<br \/>&nbsp;&nbsp; $limit = 800<br \/>&nbsp;&nbsp; $procs = Get-Process |where {$_.handles -ge $limit}<br \/>&nbsp;&nbsp; foreach ($p in $procs)<br \/>&nbsp;&nbsp; {&nbsp; <font color=\"#ff0000\">&#8220;`n`t{0}&#8221;<\/font> -f $p.Name.ToUpper()<br \/>&nbsp;&nbsp; }<br \/>)<br \/><font color=\"#ff0000\">Jeffrey likes to say, &#8220;I LOVE HERE-STRINGS&#8221;<br \/><\/font><\/font><font face=\"Courier New\" size=\"1\"><font color=\"#ff0000\">&#8220;@<br \/><\/font><br \/>PS&gt; .\\t.ps1<br \/><font color=\"#ff0000\">&#8220;Cmds&#8221;<\/font> with &gt; 800 handles are:<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CCMEXEC<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CSRSS<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IEXPLORE<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IEXPLORE<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LSASS<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OUTLOOK<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; POWERSHELL<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PS<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SEARCHINDEXER<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SVCHOST<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SYSTEM<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WINLOGON<br \/>Jeffrey likes to say, <font color=\"#ff0000\">&#8220;I LOVE HERE-STRINGS&#8221;<\/font><\/font><\/p>\n<p>Here strings are an AWESOME tool for creating HTML or XML documents.&nbsp; <\/p>\n<p>Have a blast!<\/p>\n<p>Jeffrey Snover [MSFT]<br \/>Windows PowerShell\/Aspen Architect<br \/>Visit the Windows PowerShell Team blog at:&nbsp;&nbsp;&nbsp; <a href=\"http:\/\/blogs.msdn.com\/PowerShell\">http:\/\/blogs.msdn.com\/PowerShell<\/a><br \/>Visit the Windows PowerShell ScriptCenter at:&nbsp; <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\">http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PSMDTAG:FAQ: What is the difference between single quoted and double quoted strings?&nbsp; ANSWER:&nbsp; Double quoted string expand variables and single quoted strings do not. Example: PS&gt; $x=&#8221;TEST&#8221;PS&gt; &#8220;This is a $x&#8221;This is a TESTPS&gt; &#8216;This is a $x&#8217;This is a $x &nbsp; PSMDTAG:FAQ: How do variables expand in strings? PSMDTAG:FAQ: Why don&#8217;t properties work with [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[10],"class_list":["post-9991","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-faq"],"acf":[],"blog_post_summary":"<p>PSMDTAG:FAQ: What is the difference between single quoted and double quoted strings?&nbsp; ANSWER:&nbsp; Double quoted string expand variables and single quoted strings do not. Example: PS&gt; $x=&#8221;TEST&#8221;PS&gt; &#8220;This is a $x&#8221;This is a TESTPS&gt; &#8216;This is a $x&#8217;This is a $x &nbsp; PSMDTAG:FAQ: How do variables expand in strings? PSMDTAG:FAQ: Why don&#8217;t properties work with [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/9991","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=9991"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/9991\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=9991"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=9991"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=9991"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}