Welcome to MSDN Blogs Sign in | Join | Help

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

 

Published Saturday, July 15, 2006 7:26 PM by PowerShellTeam
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Interesting Finds: July 15, 2006

Saturday, July 15, 2006 10:05 PM by Jason Haley

# re: Variable expansion in strings and here-strings

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>

Sunday, February 11, 2007 10:53 AM by Jtb

# re: Variable expansion in strings and here-strings

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

Thursday, April 12, 2007 12:38 AM by Suresh

# Re: passing parameters to an external program using PowerShell variables

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

Wednesday, July 25, 2007 4:49 PM by Latest Newsgroup Posts

# SPI + PowerShell

Provide a User Interface to administer Sharepoint content using PowerShell.

Wednesday, September 19, 2007 8:41 PM by tom.shirley's blog

# SPI + PowerShell

Provide a User Interface to administer Sharepoint content using PowerShell.

Thursday, September 20, 2007 2:46 AM by tom.shirley's blog

# Check it out: Out-vCard

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

Wednesday, November 07, 2007 4:51 PM by Windows PowerShell

# Check it out: Out-vCard

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

Wednesday, November 07, 2007 5:21 PM by Noticias externas

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker