paul543 paul543 - 1 year ago 80
PowerShell Question

Editing and merging xml files without modification of input files

My task is to merge two xml files together. Before merging these two files, I need to remove the first line of the second file. I was able to receive the needed output file by writing these two lines:

#case if both files exists - remove first line from the file
(Get-Content $JfilePath | Select-Object -Skip 1) | Set-Content $JfilePath

#mergeFiles together
Get-Content $MfilePath, $JfilePath | Set-Content $mergedFile

The issue is that I am modifying the second file by executing the first cmdlet. I would like to keep both files in original form. I dont want to also create any temporary files.
I was trying to perform the following:

Get-Content $MfilePath, (Get-Content $JfilePath | Select-Object -Skip 1) | Set-Content $mergedFile

but I received the error:

Get-Content : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'LiteralPath'. Specified method is not supported.

Could you please help how the output file could be received without modifying these input files?

Answer Source

Try this:

(Get-Content $MfilePath), (Get-Content $JfilePath | Select-Object -Skip 1) | Set-Content $mergedFile