Powershell

PowerShell format string

String variables are frequently used in programming languages to manipulate text. In PowerShell, there are several ways to describe or define a string variable. The format string in PowerShell allows you to extend the functionality of a string variable by using placeholders. Any variable can be contained in these placeholders, and then these placeholders are embedded into a string. The Format-String concept is like the String interpolation. In this article, we will enlighten the possible ways to format a string in PowerShell.

How to format a string in PowerShell

This section contains the possible ways to perform string formatting in PowerShell. A description of each method is provided along with an example:

Expand a string

The primary way to get a Format string is by expanding it with the help of placeholders. Here, we will provide a demonstration of how a string can be extended. So, let’s dig into the string formatting process:

Firstly, we have stored a string in a variable named $make that contains the value “HP”. The variable storage refers to the command written below:

> $make="HP"

After variable creation, we have used it in another string variable as can be seen in the command written below. Once the command is executed, you would observe that the string value has been expanded by the $make variable.

> Write-Output "$make is the leading laptop manufacturer company"

Format function

The format function resides in the .Net string class and the following syntax helps in using it:

 [String] ::Format(<"string-instance">)

The <string-instance> in the above syntax contains the string that is going to be formatted using the format function.

To exercise the format function, a string variable is created ($svar) and the linuxhint value is stored in it:

> $svar="linuxhint"

After that, the format function is applied in the following way:

> [String]::Format("Welcome to $svar")

Format operator

The format operator also provides support for string formatting and is quite helpful in placing more than one string variable. Let’s practice its usage by following the below-stated example:

Firstly, two string variables $sv1 and $sv2 are declared by using the following commands:

> $sv1="Welcome to"
> $sv2="linuxhint"

Once the variables are declared the format operator (-f) is used with a string as shown below. The {0} and {1} are the indexes of the string variables and these index numbers are obtained from the order of variables after the format(-f) operator. For example, the {0} and {1} refers to $sv1 and $sv2 respectively.

> "Hello! And {0} {1}" -f $sv1, $sv2

Apart from the multivariable specialty of the format operators, it can also be used to format a string that contains one variable. For instance, a string variable is declared and used in a string instance followed by a format operator.

> $sv="Ubuntu"
> "{0} is a Debian-based distro of Linux" -f $sv

Format operator and Format function

The format operator and format function can be joined to get a formatted string. The Format operator is used before/after the of a format function.

Here again two variables are declared $v1 and $v2:

> $v1="string formatting"
> $v2="is being performed"

The format function and format operator can be applied in the following way.

> [String]::Format("Hi, its  PowerShell and {0} {1}", $v1, $v2)

Bonus Tip

It is observed that usually a string variable is used in a string instance. However, one can use multiple data types or string formatting. For instance, the command provided below shows the integration of integers and string variables for string formatting.

> $s="keyboard"
> $cn=19
> [String]::Format("The price of $s is `$$cn")

Conclusion

The format string allows you to return a formatted string by using the variables in a string instance. This article provides the possible ways to format strings in PowerShell that include string expansion, the use of format function, and the use of format operator. The string expansion method just integrates a variable in a string and the -f operator is used in the format operator method to format a string. Moreover, the format function of a .Net class uses its defined syntax for string formatting. In the end, you would also experience string formatting by using variables of multiple data types.

About the author

Adnan Shabbir