Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell append string to text creates a new line

Tags:

powershell

Im trying to create a ps script which appends the string ;history directly after ;stylesheets but currently the script is creating an additional line when i want it to append it directly after.

This is how it is suppose to look

;stylesheets;history

But this is what happens

;stylesheets
;history

Can someone explain why?

 (Get-Content K:\Temp\test.properties) | 
        Foreach-Object {
            $_ 
            if ($_ -match ";stylesheets") 
            {   ";history"
            }
        } | Set-Content K:\Temp\test.properties
like image 792
ebrodje Avatar asked Dec 10 '25 16:12

ebrodje


2 Answers

Alternative solution:

(Get-Content K:\Temp\test.properties).Replace(';stylesheets',';stylesheets;history') |
Set-Content K:\Temp\test.properties
like image 100
G42 Avatar answered Dec 13 '25 07:12

G42


You are getting two lines because you are returning two objects when you match. @($_,";history") and each object is written as a line. You could combine them into one string when you match.

(Get-Content K:\Temp\test.properties) | 
    Foreach-Object {
        if ($_ -match ";stylesheets") {
            "$_;history"
        } else {
            $_
        }
    } | Set-Content K:\Temp\test.properties
like image 25
BenH Avatar answered Dec 13 '25 09:12

BenH