PowerShell: How to Print Multiple Variables on Same Line


You can use the following syntax in PowerShell to print multiple variables on the same line:

"$var1 $var2 $var3"

This particular example will print the variables $var1, $var2 and $var3 all on the same line in the output.

By wrapping the variable names in quotes, we are able to use string interpolation to display the values of each variable in consecutive order on one line.

If you’d like to include a separator between the variables (such as a comma) then you can also easily do so:

"$var1, $var2, $var3"

The following example shows how to use this syntax in practice.

Example: How to Print Multiple Variables on Same Line in PowerShell

Suppose we create the following three variables in PowerShell:

$var1 = "How"
$var2 = "are"
$var3 = "you"

Now suppose that we would like to print each of these variables on the same line in the output.

We can use the following syntax to do so:

"$var1 $var2 $var3"

The following screenshot shows how to use this syntax in practice:

PowerShell print multiple variables on same line

Notice that the values of all three variables are printed on the same line to produce the following result:

  • How are you

Also note that we could include a separator in the output between the variables if we’d like.

For example, we can use the following syntax to print each variable on the same line, separated by a comma:

"$var1, $var2, $var3"

The following screenshot shows how to use this syntax in practice:

PowerShell print multiple variables on same line with separator

Notice that the values of all three variables are printed on the same line, separated by a comma, to produce the following result:

  • How, are, you

Feel free to use similar syntax to print as many variables as you’d like on the same line.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Concatenate Strings
PowerShell: How to Append String to Each Item in Array
PowerShell: How to Get First Line of String in a Variable

Leave a Reply