3

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

2 Answers 2

3

Alternative solution:

(Get-Content K:\Temp\test.properties).Replace(';stylesheets',';stylesheets;history') |
Set-Content K:\Temp\test.properties
Sign up to request clarification or add additional context in comments.

1 Comment

@ebrodje This is the more elegant/efficient approach. Especially since you mention that you want ;history to always be directly after ;stylesheets. My solution of looping with match will always put ;history at the end of the line. In your example, those cases are the same but they may not always be.
3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.