Concatenate, Expand, Format and All Things PowerShell Strings

As a collection of sequential characters, a string represents a meaningful text. A PowerShell string is a “System.Strings” object. These strings can be concatenated, expanded, or formatted using several PowerShell cmdlets. Strings are wrapped within single or double quotes to display in PowerShell. The string within double quotes supports expansion while the strings within single quotes only show literal strings.

This write-up will observe methods to modify a string in PowerShell.

How to Concatenate, Expand, and Format Strings in PowerShell?

The below-mentioned operations will be discussed:

Method 1: Concatenate a String in PowerShell

The concatenation process involves joining two or more strings together. It is performed to create a text that is meaningful. PowerShell uses the “+” operator to concatenate a string.

Example: Concatenating a String Using the “+” Operator

This example will give a demonstration to join or concatenate a string:

$str1 = "Hello "
$str2 = "World"
$string = $str1 + $str2
Write-host "The string is concatenated."


According to the above code:

  • First, initialize two variables “$str1”, and “$str1”. Then, assign each of them two different strings.
  • After that, initiate another variable “$string” and specify the above-created string and a “+” sign between them to concatenate them.
  • Then, specify the “Write-Host” cmdlet to add a string line and call the “$string” variable:

Method 2: Expand a String in PowerShell

The strings are expanded to get the shortest code to get the string. This works in such a way that strings to be expanded are enclosed within double inverted quotes.

Example: Expanding a String Using ” “

Now, expand a string in PowerShell:

$fname = "John"
$lname = "Doe"
"$fname $lname"


According to the above code:

  • First initiate two different string assigned variables.
  • After that, write both of the variables within double inverted quotes separated by a space:

Method 3: Format a String in PowerShell

The formatting of the strings in PowerShell is performed using the “-f” operator. It contains three parts, the placeholders, such as “{0} {1}”, itself (-f), and the string assigned variables separated by a comma.

Example: Formatting a String Using the “-f” Operator

This example will format a string in PowerShell:

$fname = "John"
$lname = "Doe"
"{0} {1}" -f $fname,$lname
"{1} {0}" -f $fname,$lname


According to the above code:

  • After initializing two string variables, add the index numbers according to the number of variables.
  • For instance, there are two variables, so the index numbers 0, and 1 will be utilized.
  • To format a string, write the two index numbers which are 0 and 1, each enclosed inside medium curly braces within inverted quotes.
  • Then, add the “-f” parameter to format the string followed by the string assigned variables separated by a comma:

That was all about concatenating, expanding, and formatting strings in PowerShell.


A string in PowerShell is associated with the .Net family of “System.Strings”. It is a collection of sequential characters that make a meaningful text. The strings can be modified using several specific PowerShell cmdlets. This blog has discussed a guide to concatenate, format, replace, or expand a string in PowerShell.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.