How do I remove spaces in Powershell?

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 from a string in Powershell using replace and regular expression.

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.

Replace space in a string in Powershell using replace operator.

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 space from a string in Powershell using the replace method.

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.

Remove spaces from a string using System.String class and Split method.

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 useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Rajesh Dhawan

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.