Powershell offers multiple cmdlets and methods to remove spaces from a string. The process is quick and easy. We look at some simple ways by which you can remove spaces from a string in Powershell.
Introduction
Before we discuss the ways to remove spaces from a string, we will create a string and assign it to a variable. So, we being this tutorial with a string assignment to variable $str1 as per the following command.
$str1=”Techepages is a website for Powershell tutorials.”
Now that we have assigned a string, we will work on the ways by which we can remove spaces from within the string in Powershell. We will look at four different ways by which we can remove spaces from a string in Powershell.
Remove spaces in Powershell using Replace operator and regex
We can remove spaces from a string by using the replace operator and regular expression \s for whitespace.
The command looks like the one below:
$str1=”Techepages is a website for Powershell tutorials.”
$str1 -replace “\s”, “”
The output of the command is displayed below as a screenshot.

Remove spaces in Powershell using Replace operator
We will remove spaces from a string using another combination of the replace operator. The following command will remove spaces from a string in Powershell:
$str1=”Techepages is a website for Powershell tutorials.”
$str1 -replace ” “, “”
This is a simple command and its output is represented below in the screenshot. Space within the string has been removed by virtue of the command stated above.

Remove spaces in a string in Powershell using replace method
Apart from using the replace operator to remove spaces in a string, we can also use the replace method to remove spaces. The command below will remove spaces from a string in Powershell using the replace method.
$str1=”Techepages is a website for Powershell tutorials.”
$str1.replace(” “,””)
The output of this command is represented in the screenshot below:

Remove spaces in a string using System.String Split class
System.String is a .NET class and we can use the split method to remove spaces from a string in Powershell. The following command will take a string and remove spaces from the string.
$str1=”Techepages is a website for Powershell tutorials.”
$str1.Split(‘ ‘,[System.StringSplitOptions]::RemoveEmptyEntries) -join ‘ ‘
The output of this command is given in the screenshot hereunder.

Summary
In this Powershell tutorial, we have seen four different ways by which we can remove spaces from within a string. For backward compatibility with older versions of Powershell, you can use the System.String class.
These methods can be used to remove spaces that exist within the string. For removing header or trailing spaces from a string, there are separate methods and we discuss these in separate tutorials.
Suggested Powershell Tutorials
The following Powershell tutorials will help you in performing basic system administration tasks on Windows computers.
- How to find disk block size in Powershell?
- How to list installed printers in Powershell?
- How to find current directory path in Powershell?
- How do I convert epoch time to date in Powershell?
- How to find the public IP address using Powershell?
- How to find the boot directory using Powershell?
- How to find DoH DNS Servers using Powershell?
- How to get sid from the user account in Powershell?
- How to get system boot time in Powershell?
- How to append to a file in Powershell?
Rajesh Dhawan is a technology professional who loves to blog about smart wearables, Cloud computing and Microsoft technologies. He loves to break complex problems into manageable chunks of meaningful information.