Understanding PowerShell and Basic String Formatting

Summary: Microsoft Scripting Guy, Ed Wilson, talks about understanding Windows PowerShell and basic string formatting.

 Hey, Scripting Guy! QuestionHey, Scripting Guy! I am really confused. It seems that at times you pull off some really wild string formatting, and I have yet to see it documented very well. I have your new Windows PowerShell 3.0 Step by Step book on order, but it has not yet arrived, so I do not know if you talk about this there. Even if you do, I would like you to help me out with this because it looks really powerful—but it also seems nearly impossible to understand.


Hey, Scripting Guy! Answer Hello VT,

Microsoft Scripting Guy, Ed Wilson, is here. This morning my day started with an early visit to the dentist. Although my dentist is nice, the worst thing about my visit was when I was leaving she said, “No tea for at least six hours.” Major bummer.

Intro to Windows PowerShell composite formatting

Windows PowerShell provides access to the .NET Framework composite formatting feature. Composite formatting takes a list of objects and a composite formatting string as input. These consist of indexed place holders that are called format items. The output is a string that consists of the original text intermixed with the string representations of the objects in the list.

Composite formatting methods

Several methods support the composite formatting feature, and there are many different formatting types available.

Note   For reference information about this technique, see Composite Formatting on MSDN.

Example 1: Use static Format method

The System.String .NET Framework class has a Format static method that accepts composite formatting techniques. The following illustrates using this technique.

PS C:> [string]$name = ‘Scripting Guy’

PS C:> [string]::Format(“Name = {0}”,$name)

Name = Scripting Guy

Example 2: Use the Format operator

By using the -f Windows PowerShell format operator, I can accomplish the same thing. This is shown here.

PS C:> [string]$name = ‘Scripting Guy’

PS C:> “Name = {0}” -f $name

Name = Scripting Guy

Example 3: Use an Expanding string

Of course, I do not have to use composite formatting, because with Windows PowerShell I can use an expanding string, to accomplish the same thing. This is shown here.

PS C:> [string]$name = ‘Scripting Guy’

PS C:> “Name = $name”

Name = Scripting Guy

Understanding the three string techniques

In the previous three examples, the outcome is exactly the same—the string “Name = Scripting Guy” returns from the command. So which one is best to use? From an understandability perspective, I think the Windows PowerShell format operator syntax (example 2) is the least intuitive, and next is the Format static method (example 1). The most understandable is the expanding string (example 3).

But even the expanding string is not exactly intuitive because it is unexpected behavior, and it can be confused with non-expanding strings (that use single quotation marks). In more complex examples, using an expanding string becomes very hard to read, and it can be error prone. So for something simple like substituting a name in a string for something supplied via a variable, it is fine. But for more complex string formatting, I use composite formatting. So I want to talk about that a bit more.

How does composite formatting work?

Example 1 and example 2 use composite formatting. The format item is “{0}”, and it has an index of 0. This index component is mandatory, and it is sometimes called a parameter specifier (I often use this term). Note that the index always begins with a number starting from 0 and it corresponds to the items in the list of objects. Therefore, an item using parameter specifier 0 corresponds to the first object in the list of objects. In examples 1 and 2, the object list is simply the variable $name. It is possible to use multiple format items. Examples 4, 5, and 6 illustrate this technique.

Example 4: Use the static Format method with two format items

In this example, I assign strings to the variables $name and $statement. In the formatted output, I want to use the static strings The and thinks that in addition to the exclamation point ! in the output. I also substitute values stored in the two variables. The only thing that is a bit confusing is the multiple commas that appear in the method signature.

PS C:> [string]$name = ‘Scripting Guy’

PS C:> [string]$statement = ‘PowerShell rocks’

PS C:> [string]::Format(“The {0} thinks that {1}!”,$name,$statement)

The Scripting Guy thinks that PowerShell rocks!

Example 5: Use the Format operator and two format items

After you figure out how to read the Windows PowerShell Format operator, it is easier to use. This is because when you learn to look for the list of objects on the right side of the –f and the composite formatting string on the left side of the –f operator, it becomes easy to read and understand. It is actually easier that reading the complex method call used by the Format static string method.

PS C:> [string]$name = ‘Scripting Guy’

PS C:> [string]$statement = ‘PowerShell rocks’

PS C:> “The {0} thinks that {1}!” -f $name, $statement

The Scripting Guy thinks that PowerShell rocks!

Example 6: Use an expanding string with two format items

Using an expanding string with two format items becomes a bit more complicated to read. This is because the variables and the static string items merge, such as $statement! I was not certain this would work properly until I tried it (it could have required escaping).

PS C:> [string]$name = ‘Scripting Guy’

PS C:> [string]$statement = ‘PowerShell rocks’

PS C:> “The $name thinks that $statement!”

The Scripting Guy thinks that PowerShell rocks!

VT, that is all there is to start using compositing formatting. Formatting Week will continue tomorrow when I will talk about some of the advanced uses of composite formatting. There is some really cool stuff coming up tomorrow, and you will not want to miss it.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Comments (9)

  1. jrv says:



    PS>$a = @('{0}','{1}','!')

    PS>$b=@('Real', 'cool')

    PS>$a -f $b

  2. mredwilson says:

    @jsclmedave Thanks for your comment. I have received many questions and comments about using formatting strings and the like, so that prompted me to write this article. I will be talking about formatting all this week — and I am really excited about the series. I actually learned a few things myself while writing the articles. They are good stuff. Thanks for your comment. Hope you enjoy the series.

  3. jrv says:

    @Ed – I am guessing that you are planning more on this.  It is , in my opinion, one of the best and least used features of PowerShell.

  4. jsclmedave says:

    It is these seemingly simple examples that I find stands in the way of many from really taking off with Powershell.  Including myself.  : )  Thanks Ed!

  5. mredwilson says:

    @JRV yep — all this week I am talking about this feature. Some pretty cool stuff actually! I am glad you like the series so far.

  6. mredwilson says:

    @A humble request Hmm … I had not thought about making a PowerShell Scriptomatic for the Surface RT. Unfortunately, I am really swamped and I can confidently say it will not happen for more than 6 months — if ever. There is nothing to stop someone from the community from making one however. In addition, with the new CIM cmdlets there is much less need for using the Scriptomatic because cmdlets like Get-CimClass make it easy to discover WMI classes. This is what the Scriptomatic does. In addition, each of the help examples have great examples on how to use it. On your Surface RT, open the PowerShell prompt and type Update-Help to get the latest help.

  7. mredwilson says:

    @J Turnage yes this is a cool addition. Thanks for sharing.

  8. J Turnage says:

    At the expense of making it a little less readable, you can also do some extra variable formatting in the string by wrapping it with $().  

    Eg, your date formatting from earlier…

    "Today's date is $((get-date).ToString("MM-dd-yy"))."

    Probably a terrible example since it can be extracted out into a -f param or any of the other ways, but there are some times where it's useful to do it inline.

  9. A humble request says:


        Hi there. I'm writing today to ask the impossible…. a favor to ask from a random internet user… Don't delete me yet!  I know it sounds dubious, but I was hoping it might be possible to ask for a version of Scriptomatic apps for RT, please. I am trying to learn Powershell and it's been suggested that I look into Scriptomatic, but I can't install it at work (rules) and now I can't take it with me since it won't load on RT, either. So here I am, a n00b throwing himself on the mercy of the… coders?  Oh please sir, in my best Oliver voice, won't you compile some more?  

    Thanks for reading and at least considering. 🙂

    Steven B (aka SciaticNerd@gmail.com)