I am a noob at powershell and still trying to learn scripting but stumbling on the basics. I have a text file that contains a list of websites
Default Websites
Power Shell
Hello World
I need to remove the white spaces between the words so I get the outcome below
DefaultWebsites
PowerShell
HelloWorld
I have tried using the Trim('') command but it is not removing the white spaces.
Here is my original script that removes all white spaces at the beginning and the end and any blank lines. what do I need to add to the script to remove the white spaces in between words?
Get-WmiObject -Class IIsWebServerSetting -Namespace "root\microsoftiisv2" | Select ServerComment | Format-Table -HideTableHeaders | out-file c:\website.txt
$b = Get-Content -Path C:\website.txt
$b | ForEach {$_.TrimEnd()} | ? {$_.trim() -ne '' } > C:\website.txt
Thank you in advance. I appreciate any help.
You can use the -replace
command with a simple regex:
-replace '\s'
\s match any white space character [\r\n\t\f ]
Try this:
[System.IO.File]::WriteAllText(
'C:\website.txt',
([System.IO.File]::ReadAllText('C:\website.txt') -replace '\s')
)
I would go with this approach (where the hero is Replace function):
Get-WmiObject -Class IIsWebServerSetting -Namespace "root\microsoftiisv2" | Select ServerComment | Format-Table -HideTableHeaders | out-file c:\website.txt
$b = Get-Content -Path C:\website.txt
@(ForEach ($a in $b) {$a.Replace(' ', '')}) > C:\Website2.txt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With