Skip to main content
TopMiniSite

Back to all posts

How to Remove Space When Combining Variables In Powershell?

Published on
3 min read
How to Remove Space When Combining Variables In Powershell? image

Best PowerShell Tools to Buy in October 2025

1 Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

BUY & SAVE
$47.34 $59.99
Save 21%
Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools
2 Beginner’s Guide to PowerShell Scripting: Automate Windows Administration, Master Active Directory, and Unlock Cloud DevOps with Real-World Scripts and Projects

Beginner’s Guide to PowerShell Scripting: Automate Windows Administration, Master Active Directory, and Unlock Cloud DevOps with Real-World Scripts and Projects

BUY & SAVE
$0.99
Beginner’s Guide to PowerShell Scripting: Automate Windows Administration, Master Active Directory, and Unlock Cloud DevOps with Real-World Scripts and Projects
3 PowerShell for Penetration Testing: Explore the capabilities of PowerShell for pentesters across multiple platforms

PowerShell for Penetration Testing: Explore the capabilities of PowerShell for pentesters across multiple platforms

BUY & SAVE
$47.49 $49.99
Save 5%
PowerShell for Penetration Testing: Explore the capabilities of PowerShell for pentesters across multiple platforms
4 Learn PowerShell Scripting in a Month of Lunches

Learn PowerShell Scripting in a Month of Lunches

BUY & SAVE
$49.71
Learn PowerShell Scripting in a Month of Lunches
5 Troubleshooting SharePoint: The Complete Guide to Tools, Best Practices, PowerShell One-Liners, and Scripts

Troubleshooting SharePoint: The Complete Guide to Tools, Best Practices, PowerShell One-Liners, and Scripts

BUY & SAVE
$27.00 $59.99
Save 55%
Troubleshooting SharePoint: The Complete Guide to Tools, Best Practices, PowerShell One-Liners, and Scripts
6 AWS Tools for PowerShell 6: Administrate, maintain, and automate your infrastructure with ease

AWS Tools for PowerShell 6: Administrate, maintain, and automate your infrastructure with ease

BUY & SAVE
$48.99
AWS Tools for PowerShell 6: Administrate, maintain, and automate your infrastructure with ease
+
ONE MORE?

To remove space when combining variables in PowerShell, you can use the -join operator to concatenate the variables. For example, if you have two variables $var1 and $var2 that you want to combine without space, you can use $var1 + $var2 to concatenate them. Alternatively, you can use $var1 + $var2.Trim() if you want to remove any leading or trailing spaces before combining the variables. This will allow you to join the variables without any additional spaces between them.

What is the impact of whitespace on variable concatenation in PowerShell?

In PowerShell, whitespace can impact variable concatenation in different ways depending on the context in which it is used.

  1. If whitespace is used between variables to concatenate them, it will be interpreted as a literal space character in the resulting output. For example, if you have two variables $var1 and $var2 and you concatenate them with whitespace like this: "$var1 $var2", the resulting output will be the values of $var1 and $var2 separated by a space.
  2. If you want to concatenate variables without adding a space between them, you can use the concatenation operator (+) without any whitespace. For example, "$var1$var2" will concatenate $var1 and $var2 without any space between them.
  3. Whitespace can also be used to format the output of concatenated variables for readability. By adding whitespace before or after variables, you can control the spacing and alignment of the output.

Overall, the impact of whitespace on variable concatenation in PowerShell is mainly related to how it affects the formatting and spacing of the output. It is important to be mindful of how whitespace is used when concatenating variables to ensure the desired output is achieved.

How to remove trailing spaces in PowerShell?

You can remove trailing spaces in PowerShell using the TrimEnd() method. Here's an example:

$myString = "Hello World " $trimmedString = $myString.TrimEnd() Write-Host $trimmedString

This will output:

Hello World

Alternatively, you can use the built-in Trim() method to remove both leading and trailing spaces:

$myString = " Hello World " $trimmedString = $myString.Trim() Write-Host $trimmedString

This will also output:

Hello World

How to strip spaces from a variable in PowerShell?

To strip spaces from a variable in PowerShell, you can use the following command:

$var = $var -replace '\s',''

This command uses the -replace operator to remove all white spaces (including spaces, tabs, and newlines) from the variable $var. The regular expression pattern '\s' matches any whitespace character, and the replacement string is an empty string, effectively removing all whitespace from the variable.