fbpx

PowerShell String Format | 2 Best Ways To Use Windows Format Operator (-f)

The versatility of String format allows you to complete tasks in a variety of ways. Most likely, instead of combining all of them in one screenplay, you would stay with the one that makes you feel the most at ease.

PowerShell String Format | 2 Ways To Use Windows Format Operator (-f)

You will eventually need to work with strings that contain variables when scripting with PowerShell. There are various approaches you can take for String format in PowerShell, depending on the circumstances.  keep reading to know more about PowerShell String Format and how to use it.

When to Use the String Format Operator?

The fact that there is no requirement to match an index with a value makes it more common for people to insert variables inside of strings and expand them. There are exceptions, such as when special characters are involved that need to be escaped.


Let’s imagine you need to run a command-line tool that needs a filename as a parameter. You must enclose the reference in double quotes since the file path may contain spaces. Utility.exe would not have been able to appropriately interpret the file parameter without those double quotes.

Utility.exe -file "C:\Program Files\Folder\subfolder"
Utility.exe -file "C:\Program Files\Folder\subfolder"

Now suppose you wish to accomplish this with a PowerShell script. It might appear a little disorganized if you were using expanding strings because you need to use a backtick to get the double quotes out.

$FileName = 'C:\Program Files\Folder\subfolder'
Start-Process -FilePath 'utility.exe' -Args "`"$FileName`""
PowerShell format string

Let’s try to get the same outcome with the String Format operator now.

$FileName = 'C:\Program Files\Folder\subfolder'
$Args = '"{0}"' -f $FileName
Start-Process -FilePath 'utility.exe' -Args $Args
$FileName = 'C:\Program Files\Folder\subfolder'
$Args = '"{0}"' -f $FileName
Start-Process -FilePath 'utility.exe' -Args $Args

No escape characters are required for users. This example might appear straightforward, but once you encounter lengthy strings that demand a lot of escaping, you will quickly realize the format operator’s true value.

PowerShell format String: Basics

The most common type of variable in PowerShell is undoubtedly the string type. Strings are excellent for defining simple character sets, such as a machine name, text file path, registry keypath, or URL. You can perform almost anything with PowerShell’s string format features and by expanding variables inside of strings.

When one or more characters are enclosed in single or double quotes, a String Format is defined, as in:

$Url = 'http:\\www.google.com'
$FilePath = 'C:\sample.txt'
PowerShell format string

It’s simple to define strings in this way, but eventually, you’ll need to access a variable’s value within strings.

Perhaps you have a script with a ComputerName parameter, and you need to link a common prefix like SRV to it in your script.

$ComputerName = 'ALPHA'
$ComputerName = "SRV-$ALPHA"
PowerShell format string computer names

The value of the $ComputerName variable has now changed to SRV-ALPHA, however, there are other ways to introduce variables in strings besides wrapping them in double quotes.

Expanding The Windows Powershell Strings 

The first technique simply involves adding variables to already-written command. This is the most typical method of displaying a variable’s value in a string.

To accomplish this, the variable must be enclosed in double quotes and placed inside a string. Because single quotes take all characters literally, they will not be effective.

I must, for instance, specify the location of a file. I am aware of the path to the root folder but not the file name as of now. The folder path may be statically entered into a variable named $FullPath, whereas the file name would be variable.

$FileName = 'MyFile.txt'
$FullPath = "C:\Folder\subfolder\$FileName"
$FullPath
C:\Folder\subfolder\MyFile.txt
Expanding The Windows Powershell Strings 

What if you need to specify a string’s contents as an object property? This appears a little unique. In this scenario, I need to parenthesize the property and add a dollar sign before it. An operator for subexpressions is what this is. Windows PowerShell is instructed to expand the value first by this.

$File = Get-Item -Path 'C:\MyFile.txt'
$FullPath = "C:\Folder\subfolder\$($File.Name)"
Expanding The Windows Powershell Strings 
$FullPath
C:\Folder\subfolder\MyFile.txt
Expanding The Windows Powershell Strings 

Are There Any Alternative Ways to Format Strings in PowerShell?

PowerShell offers various powerful PowerShell splatting techniques to format strings. These methods provide alternative ways to manipulate and structure strings within the code. By utilizing these techniques, developers can easily handle complex string formatting tasks and improve the efficiency of their PowerShell scripts.

How To Use The PowerShell String Format Operator

Using the String Format operator (-f syntax) in Powershell is another approach to guarantee that variable values are inserted into your strings.

It is not necessary to enclose the variable or property in double quotes when using the String Format operator. As demonstrated below, you can create a string enclosed in single quotes and define the variables that will be included outside of it.

$File = Get-Item -Path 'C:\MyFile.txt'
'C:\Folder\subfolder\{0}' -f $File.Name
C:\Folder\subfolder\MyFile.txt
Expanding The Windows Powershell Strings 

As you can see, it has the same result. However, this time you didn’t need to bother about using double quotes, parentheses, or another dollar sign to enclose the object property. It may be more difficult to grasp, but it looks a little cleaner. When using the format operator, $File must be used in place of the value “0”.Imagine the name.

Simply increase the value of your labels by 1, then keep adding variables to the format operator, separating them with commas. You can include as many references as you like, provided that the index position of the format operator coincides with the location in the string label.

$File = Get-Item -Path 'C:\MyFile.txt'
$SubFolderName = 'folder1'
'C:\Folder\subfolder\{0}\{1}' -f $SubFolderName, $File.Name
C:\Folder\subfolder\folder1\MyFile.txt
Expanding The Windows Powershell Strings