Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7001499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:48:39+00:00 2026-05-27T20:48:39+00:00

I was trying to find a way in powershell to remove more than one

  • 0

I was trying to find a way in powershell to remove more than one white space.

But what i found is how to do it in php. “Removing more than one white-space“

There will be similar regular expression may available .

How to acheive the same in powershell?

My string is like this

Xcopy Source  Desination

Some lines may contain more than one white space between Source and destination.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:48:39+00:00Added an answer on May 27, 2026 at 8:48 pm

    If you’re looking to collapse multiple consecutive whitespace characters into a single space then you can do this using the -replace operator. Given…

    PS> $beforeReplace = '   [   Hello,   World!   ]   '
    PS> $beforeReplace
       [   Hello,   World!   ]   
    PS> $beforeReplace.Length
    29
    

    …you would call the -replace operator like this…

    PS> $afterReplace = $beforeReplace -replace '\s+', ' '
    PS> $afterReplace
     [ Hello, World! ] 
    PS> $afterReplace.Length
    19
    

    The first parameter to -replace is a regular expression pattern to match, and the second parameter is the text that will replace any matches. \s will match a whitespace character, and + indicates to match one or more occurrences, so, in other words, one or more adjacent whitespace characters will be replaced with a single space.

    Replacement without whitespace normalization

    If you don’t need to normalize all whitespace characters to spaces and, thus, it’s ok for standalone whitespace characters to be left untouched, then for long strings you might see better performance with this variation…

    PS> $afterReplace = $beforeReplace -replace '\s{2,}', ' '
    PS> $afterReplace
     [ Hello, World! ] 
    PS> $afterReplace.Length
    19
    

    The \s{2,} uses a quantifier meaning "match the preceding element at least two times"; therefore, standalone whitespace characters will not be replaced. When the input string contains a mix of whitespace characters…

    PS> $beforeReplace = "1Space: ;2Space:  ;1Tab:`t;2Tab:`t`t;1Newline:`n;2Newline:`n`n;"
    PS> $beforeReplace
    1Space: ;2Space:  ;1Tab:    ;2Tab:      ;1Newline:
    ;2Newline:
    
    ;
    PS> $beforeReplace.Length
    57
    

    …note how the results for the two approaches differ…

    PS> $afterReplaceNormalized = $beforeReplace -replace '\s+', ' '
    PS> $afterReplaceNormalized
    1Space: ;2Space: ;1Tab: ;2Tab: ;1Newline: ;2Newline: ;
    PS> $afterReplaceNormalized.Length
    54
    PS> $afterReplaceUnnormalized = $beforeReplace -replace '\s{2,}', ' '
    PS> $afterReplaceUnnormalized
    1Space: ;2Space: ;1Tab: ;2Tab: ;1Newline:
    ;2Newline: ;
    PS> $afterReplaceUnnormalized.Length
    54
    

    While both yield strings of the same length, the unnormalized replacement leaves the single space, single tab, and single newline whitespace runs unmodified. This would work just the same whether adjacent whitespace characters are identical or not.

    Additional documentation

    • Enter help about_Comparison_Operators [ Windows PowerShell 2.0 ] [ PowerShell (Core) ]
    • Enter help about_Regular_Expressions [ Windows PowerShell 2.0 ] [ PowerShell (Core) ]
    • .NET Regular Expression Language – Quick Reference
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to find a way to put some transparency in everything except one
I'm trying find a way to have PHP to indicate to the browser that
I'm trying find a way to create mysql users with passwords using php code,
I'm trying to find a way to display one link to an IE user
Im trying to find simplest way to merge two arrays into the third one,
Im trying to find a way of refreshing a PHP variable on a webpage
I'm trying to find a way to replace a node with one that has
Trying to find a way to send a POST HTTPS request from Python to
I'm trying to find a way to list the (static) dependency requirements of a
I'm trying to find a way to validate a large XML file against an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.