With PowerShell, there is often a need to convert some object or object property into a name in some separate namespace. For example, you might want to create a folder with the year, month, and day as part of the fonder's name. Or create a file with the current date as part of the name.

It turns out, such things are pretty straightforward, but they rely on using some of the features of the .NET Framework. For those not sufficiently advanced, you can call this PowerShell magic. For the initiated — it’s just some neat .NET formatting. Great tools for those who know how.

As you may know, PowerShell is based on the .NET Framework. The Framework contains a wide range of features you can leverage via PowerShell. In general, you should use cmdlets to do most of your work, but the .NET framework contains other features you can exploit easily when you need to.

A neat feature of .NET is the ability to create beautifully formatted strings using the Composite Formatting System (CFS). .NET uses Composite Format Strings and an array of values, and creates a nicely formatted string. This is incredibly powerful and useful in creating nice looking output.

Within the CFS, a Composite Formatting String is a string that contains some set of characters plus one or more Place Holders. A Place Holder is a location within the composite formatting string where you want .NET/PowerShell to format a value using some specific format. As always, PowerShell has defaults for these, which you can override.

In this case, you have a Composite Format String with three placeholders. This is followed by the -F operator and an array of values. The first Place Holder is noted as {0} since .NET and PowerShell begin arrays at 0. The format operator (-F) tells PowerShell to format the array following the -F into the composite format string. PowerShell takes the first value, the characters ‘string’, and inserts it at the {0} Place Holder, then formats the value 4 into the second Place Holder. Finally, the result of the third member of the array, which is an expression that is evaluated to return today’s date and time, is formatted into the last Place Holder. The output looks something like this:

Text

Composite Format String. 2+2 is 4 and today is 02/11/2017 12:32:05!!

In this example, PowerShell decided on the ‘best’ format for each of the values. In some cases, you need more control over precisely how .NET should format each Place Holder. .NET provides you with two additional options for each Place Holder. You can specify the number of characters to use in formatting the value (and whether to align the value to the left or right within those characters). Additionally, you can specify a .NET Format String to specify exactly how .NET should do the formatting.

The Place Holder syntax is as follows:

Powershell

{Index[,Alignment][:FormatString]}

The Index value tells PowerShell which array member to insert at the place holder. The Alignment value tells PowerShell how many characters to reserve for the value (truncation or padding with spaces occurs to fill out this field) and whether to right or left align the value inside the placeholder. A positive Alignment value indicates right justification, and negative indicates left justification. So if you specify:

So back to our requirement: How would you create a folder name that included the current date? You could start with this:

Powershell

"Backup_{0}"-F(Get-Date)

Which produces the following string:

Text

Backup_02/11/2017 12:40:09

This may be just what you want. But suppose you want the folder to have the name as just ‘Backup_’ followed by today’s date, with the date formatted as Year-Month-Day (eg 2017-11-02)? You just override the defaults and provide a Format String like this:

Powershell

"Backup_{0:yyyy-MM-dd}"-F(Get-Date)

The output of this string is:

Text

Backup_2017-11-02

.NET’s format strings cover a huge range of your formatting requirements. You can even apply locale specific formatting, which enables currency to reflect the local locale (for example, enabling the number 123123.11 to be formatted as £123,123.11 for the UK vs €123.123,11 in Germany).

In summary, PowerShell provides a great default formatting. But when and where you need more control, the .NET Composite Formatting System is simple and easy to leverage. You just need to know how — dazzxle your colleagues with more PowerShell magic!

Any other PowerShell tricks you'd like to share or learn? Let me know in the comments below..

8 Comments

Thomas, Thanks for the Great write up, I have always jumped thru hoops to format folder and file names in my functions this will be a Great resource and starting place for future research on other formatting Gems. :)