Prof. Powershell

Substitution Teacher

Using place holder variables in scripts to display complex text strings, is easier than you might think.

Even though PowerShell is object-oriented, that doesn't mean you give up working with text. You often need to display a text message in your scripts or functions. A simple string is no problem:

PS C:\write-host "I am PowerShell"

Or even displaying a simple variable or object property:

PS C:\> $z=get-service spooler
PS C:\> write-host $z.status
Running

However as you try building more complex strings, often with values from cmdlets or object properties, concatenating everything can get very messy very quickly. So, there's an easier way using the -f operator. The left side of a-f expression is a string that uses place holder variables. The right side of the expression is a comma-separated list where each value is inserted into the corresponding place holder. Here's an example:

PS C:\> $msg="Hello, {0}. It is now {1} on {2}." -f $env:username,(Get-Date -DisplayHint time),$env:computername

The first placeholder is {0}. This will be replaced with the value of $env:username. The second placeholder {1} will be replaced with the result of the Get-Date expression and so on. Now I can easily display this message:

PS C:\> write-host $msg
Hello, Jeff. It is now 7/24/2008 11:39:45 AM on PUCK.

You can also use the -f operator to format data. Here are some examples. I think the formatting results should be self-explanatory:

PS C:\> "{0:N}" -f 12345.6789
12,345.68
PS C:\> "{0:N0}" -f 12345.6789
12,346
PS C:\> "{0:f}" -f 12345.6789
12345.68
PS C:\> "{0:P}" -f (1287264/10mb)
12.28 %
PS C:\> "{0:T}" -f (get-date)
12:02:44 PM
PS C:\> "{0:t}" -f (get-date)
12:02 PM
PS C:\> "{0:hh:mm}" -f (get-date)
12:04

Using this approach I can create complex expressions like this:

PS C:\> $w = (Get-Process | Measure-Object -sum workingset).sum/1mb
PS C:\> $mem = (Get-WmiObject win32_computersystem).totalPhysicalmemory/1mb
PS C:\> $msg = "Hello, {0}. It is now {1:t} on {2}. Total process working set is {3:F2}MB, which is using {4:P} of available memory." -f $env:username,(Get-Date),$env:computername,$w,($w/$mem)
PS C:\>
PS C:\> Write-Host $msg
Hello, Jeff. It is now 12:15 PM on PUCK. Total process working set is 1996.58MB, which is using 60.18 % of available memory.

You can learn more about formatting types here.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular

Upcoming Training Events

0 AM
Live! 360 Orlando
November 17-22, 2024
TechMentor @ Microsoft HQ
August 11-15, 2025