String Formatting in PowerShell

PowerShell offers a couple different methods for formatting text making it easy to get the output you want. The most commonly used methods are the format operator or using composite formatting. This article will discuss both of these methods and provide some examples.

Format Operator (-F)


Typically, the format operator is used when you have a string defined that includes some indexes where you want to insert a variable and possibly do some formatting to the value. You can use multiple indexes to insert values into your string:

                                        
 #
 # Define some variables that will go into our string
 #
 $name = "John Doe"  
 $job  = "Accountant"
 
 #
 # Insert the variables into the string
 #
 "Employee: {0}, Occupation: {1}" -f $name,$job
 
[Output]

 Employee: John Doe, Occupation: Accountant


                                        

Pretty simple right? The key is to make sure you supplied enough arguments after -f to match up with the number of indexes you specified in the string. Otherwise you will see an error like this:

                                        
 Error formatting a string: Index (zero based) must be greater than or equal to zero and less
 than the size of the argument list..

                                        

You can also specify options for things like fixed-width, numeric precision, or currency formatting among others. We will look more into that in the next section.

Composite Formatting


Composite formatting is the same as using the format operator except that it looks a little different and may seem a bit more natural to .NET developers coming from C# or VB. When using this approach you use familiar .NET methods like Console.WriteLine or String.Format to specify your string and the arguments. Lets look at an example of using both:

                                        
 #
 # Define some variables that will go into our string
 #
 $name = "John Doe"  
 $job  = "Accountant"

 [String]::Format("Employee: {0}, Occupation: {1}",$name,$job)
 [Console]::WriteLine("His name is {0} and he is an {1}.",$name,$job)

[Output]

 Employee: John Doe, Occupation: Accountant
 His name is John Doe and he is an Accountant.

                                        

Now that you know how to insert values into a string lets look at specifying a string format for the inserted values. We will only look at a few here, but for a more complete list look at the article on MSDN on Composite Formatting. The following sample demonstrates how you can specify a DateTime format or a numeric format:

                                        
 $date = Get-Date
 $number = 12.99

 #
 # Write the Date in the format yyyy-MM-dd
 #
 "Today is: {0:yyyy-MM-dd}" -f $date

 #
 # Write a number as currency
 #
 [String]::Format("Amount is: {0:C}",$number)

[Output]

 Today is: 2013-12-28
 Amount is: $12.99

                                        

The previous example makes use of a couple format specifies, but there are many more that can be found in the links in the previously mentioned MSDN article. You should now have an understanding of how to format a string while inserting values from PowerShell variables.


© 2024 Embrs.net