Variable expansion in strings and here-strings

PSMDTAG:FAQ: What is the difference between single quoted and double quoted strings?  ANSWER:  Double quoted string expand variables and single quoted strings do not.


Example:


PS> $x=”TEST”
PS>
“This is a $x”
This is a TEST
PS>
‘This is a $x’
This is a $x


 


PSMDTAG:FAQ: How do variables expand in strings?


PSMDTAG:FAQ: Why don’t properties work with variable expansion in strings?


Variables get expanded in strings not property expressions.  Here is an example of a property expression that you might like to use that doesn’t work the way you might think it would:


PS> Calc
PS> $c = Get-Process Calc
PS> “Calc uses $c.Handles Handles”
Calc uses System.Diagnostics.Process (calc).Handles Handles


In this example, $c gets expanded to “System.Diagnostics.Process (calc)”.   The problem is that you wanted the $c.Handles to be expanded. 


 


 


PSMDTAG:FAQ: How do I expand an expression in a string?


Windows PowerShell will expand a variable or an expression in  a string. 
Variables look like:      $variable
Expressions look like:  $(expression)


Thus to get $c.Handles expanded you do the following:


PS> “Calc uses $($c.Handles) Handles”
Calc uses 42 Handles


Now from here, it gets even better.  You can put anything put any code you want into that expression.  You can put a 40 page script in there if you want.  Here is an example:


PS> cat t.ps1

Cmds with > 800 handles are: $(
   $limit = 800
   $procs = Get-Process |where {$_.handles -ge $limit}
   foreach ($p in $procs)
   {  ‘`n`t{0}’ -f $p.Name.ToUpper()
   }
)


PS> .\t.ps1

Cmds with > 800 handles are:
        CCMEXEC
        CSRSS
        IEXPLORE
        IEXPLORE
        LSASS
        OUTLOOK
        POWERSHELL
        PS
        SEARCHINDEXER
        SVCHOST
        SYSTEM
        WINLOGON


 


So by using $(), you can do almost anything you could want to do. 



“ALMOST  ?!!!??”


Yeah – almost but not really everything.  Notice that I used single quotes for the format string in the expression:  ‘`n`t{0}’ .  The reason I did that is that if I used double quotes, it would have caused a parser error.  I could have used escape characters but that wouldn’t help me make the point I’m making so … 


 


 


PSMDTAG:FAQ:  How do I show double quotes within a double quoted string?  ANSWER: escape them with a backtick `” or use Here-Strings.


A Here-String is a string which starts with a @” and ends with a “@ (on a line by itself).  Here-Strings can use any character you want until it sees a “@ which terminates the string. 


PS> cat t.ps1
@”
“Cmds” with > 800 handles are: $(
   $limit = 800
   $procs = Get-Process |where {$_.handles -ge $limit}
   foreach ($p in $procs)
   {  “`n`t{0}” -f $p.Name.ToUpper()
   }
)
Jeffrey likes to say, “I LOVE HERE-STRINGS”
“@

PS> .\t.ps1
“Cmds” with > 800 handles are:
        CCMEXEC
        CSRSS
        IEXPLORE
        IEXPLORE
        LSASS
        OUTLOOK
        POWERSHELL
        PS
        SEARCHINDEXER
        SVCHOST
        SYSTEM
        WINLOGON
Jeffrey likes to say, “I LOVE HERE-STRINGS”


Here strings are an AWESOME tool for creating HTML or XML documents. 


Have a blast!


Jeffrey Snover [MSFT]
Windows PowerShell/Aspen Architect
Visit the Windows PowerShell Team blog at:    http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx