Table of Contents
💡 TL;DR
To replace space with comma in PowerShell, you can use String’s replace() method or PowerShell’s replace operator.
Here is simple example:
1234567 $countries= "India China Bhutan Russia"$countriesWithComma = $countries.Replace(" ",",")$countriesWithComma#Output : India,China,Bhutan,Russia
There are multiple ways to replace Space with Underscore in PowerShell. Let’s go through them.
Using String’s replace() method
Use string’s replace()
method to replace space with comma in PowerShell.
Replace method returns new string and replaces each matching string with new string.
Replace methods takes two arguments:
- substring to find in given string :
space
- replacement String for found substring:
comma
1 2 3 4 5 6 7 |
$countries= "India China Bhutan Russia" $countriesWithComma = $countries.Replace(" ",",") $countriesWithComma #Output : India,China,Bhutan,Russia |
Using replace operator
to replace space with comma in PowerShell, use PowerShell’s replace
operator .
Replace operator takes two arguments:
- substring to find in given string :
space
- replacement String for found String:
comma
1 2 3 4 5 6 7 |
$countries= "India China Bhutan Russia" $countriesWithCommaOpr = $countries -replace " ","," $countriesWithCommaOpr #Output : India,China,Bhutan,Russia |
As you can see, replace operator replaced all spaces with comma in the String.
Please note that we have assigned result of replace() method or replace() operator to new string.
That’s all about how to replace space with comma in PowerShell.