Summary: Microsoft Scripting Guy, Ed Wilson, talks about understanding Windows PowerShell and basic string formatting.
Hey, 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.
—VT
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 Formatstatic 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 Formatstatic 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 Formatoperator, 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 Formatstatic 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