Variable expansion in strings and here-strings

Variable expansion in strings and here-strings

Rate This
  • Comments 9

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

 

Leave a Comment
  • Please add 8 and 8 and type the answer here:
  • Post
  • Why doesn't set-psdebug -strict prevent unset variables with variable expansion in strings?

    PS C:\Users\Jens> write-host "$Test"

    PS C:\Users\Jens> write-host $Test

    PS C:\Users\Jens> set-psdebug -strict

    PS C:\Users\Jens> write-host "$Test"

    PS C:\Users\Jens> write-host $Test

    The variable $Test cannot be retrieved because it has not been set yet.

    At line:1 char:16

    + write-host $Test <<<<

    PS C:\Users\Jens>

  • Why I'm getting spaces in between

    PS C:\> $OFS="";foreach ($num in 0..4) {write-host C:\SG($num)DB}

    C:\SG 0 DB

    C:\SG 1 DB

    C:\SG 2 DB

    C:\SG 3 DB

    C:\SG 4 DB

  • The best method that works for me is the use of a Here-String. You can find more information on it here:

  • Provide a User Interface to administer Sharepoint content using PowerShell.

  • Provide a User Interface to administer Sharepoint content using PowerShell.

  • Check out Dmitry Sotnikov's blog Out-vCard: Exporting Outlook Address Book . It lets you do things like:

  • Check out Dmitry Sotnikov&#39;s blog Out-vCard: Exporting Outlook Address Book . It lets you do things

  • function convertto-herestring {

    begin {$temp_h_string = '@"' + "`n"}

    process {$temp_h_string += $_ + "`n"}

    end {

       $temp_h_string += '"@'

       iex $temp_h_string

       }

    }

Page 1 of 1 (9 items)