How to Remove All Whitespace From Text in PowerShell

Use PowerShell to strip spaces, tabs, and line breaks from text with replace or regex. Includes step-by-step commands and output checks.

Before you start

This task runs locally in PowerShell on Windows, macOS, or Linux. You do not need a cloud service or administrator rights if you are only working with a text string in the current session. When you read a file, make sure you can access its location and that the file uses a text format PowerShell can read, such as UTF-8.

The examples below use the replace operator, which is available in PowerShell 5.1 and PowerShell 7. If you copy text from a web page, email, chat message, or subtitle file, the input may contain normal spaces plus other invisible characters. Decide first whether you want to remove only standard space characters or all whitespace characters such as tabs and line breaks.

Step-by-step instructions

Start with a text value assigned to a variable so the operation is easy to inspect and reuse. For example:

$text = 'Meeting ID 123 456 789'

Remove all whitespace characters

Use a regular expression that matches spaces, tabs, carriage returns, and line feeds. The pattern \s represents a single whitespace character, and the plus sign makes it match one or more in a row.

$result = $text -replace '\s+', ''

After this command, $result contains MeetingID123456789. Replacing a run of whitespace with an empty string removes the characters without inserting anything in their place.

Remove only regular spaces

If you need to keep tabs or format a file path, remove only the literal space character. The command can target a simple string instead of a regular expression:

$result = $text.Replace(' ', '')

This removes every space but leaves other whitespace untouched. Use this form when the text must stay on one line or when you only want to join words that were separated by spaces.

Process text from a file

Read the file content as one text block, then apply the same replacement. The Raw switch in PowerShell 7 reads the whole file without splitting it into lines first.

$content = Get-Content -Path 'notes.txt' -Raw
$clean = $content -replace '\s+', ''
$clean | Set-Content -Path 'notes-clean.txt' -NoNewline

In PowerShell 5.1, Get-Content also supports the Raw parameter, but check that the loaded text has the encoding you expect. The NoNewline parameter prevents Set-Content from adding an extra line break at the end of the cleaned output.

Check the output

Print the result and review it before using it in another tool:

$result

Confirm that the value contains no spaces, tabs, or line breaks where you expected them to be removed. If the text came from HTML or a rich-text editor, a non-breaking space may remain because it is not matched by \s in all .NET versions. In that case, replace the Unicode non-breaking space character explicitly after the general whitespace replacement.

For file output, check the character count or view the first part of the cleaned file. Removing whitespace does not change the sequence of letters and numbers, so compare the cleaned value by eye with a short sample of the original text.

Common problems and next steps

If the output still contains gaps, the characters may not be standard spaces. Copy the text into PowerShell and inspect it by index or look for Unicode characters that .NET treats differently. A non-breaking space can be removed with a targeted replacement such as:

$result = $text -replace '\u00A0', ''

If you want to remove only spaces at the beginning and end while keeping meaningful spaces between words, use the Trim method instead. To collapse repeated spaces into a single space, use a replacement pattern that substitutes one space rather than an empty string. Keep a copy of the original text so you can restore it if a command removed too much.