0
0
PowershellHow-ToBeginner · 2 min read

PowerShell Script to Merge Two Files Easily

Use Get-Content file1.txt, file2.txt | Set-Content merged.txt to merge two files by reading both and writing their combined content into a new file.
📋

Examples

Inputfile1.txt content: 'Hello' file2.txt content: 'World'
Outputmerged.txt content: Hello World
Inputfile1.txt content: 'Line1\nLine2' file2.txt content: 'Line3\nLine4'
Outputmerged.txt content: Line1 Line2 Line3 Line4
Inputfile1.txt content: '' file2.txt content: 'Only one file has content'
Outputmerged.txt content: Only one file has content
🧠

How to Think About It

To merge two files, read the contents of both files in order, then write all the combined lines into a new file. This way, the new file contains all lines from the first file followed by all lines from the second file.
📐

Algorithm

1
Get the content of the first file.
2
Get the content of the second file.
3
Combine the contents of both files.
4
Write the combined content into a new file.
💻

Code

powershell
Get-Content file1.txt, file2.txt | Set-Content merged.txt
Write-Output "Files merged into merged.txt"
Output
Files merged into merged.txt
🔍

Dry Run

Let's trace merging 'file1.txt' with 'Hello' and 'file2.txt' with 'World' into 'merged.txt'.

1

Read file1.txt

Content read: ['Hello']

2

Read file2.txt

Content read: ['World']

3

Combine contents

Combined content: ['Hello', 'World']

4

Write to merged.txt

merged.txt now contains: Hello World

StepActionContent
1Read file1.txtHello
2Read file2.txtWorld
3Combine contentsHello World
4Write merged.txtHello\nWorld
💡

Why This Works

Step 1: Reading files

The Get-Content command reads each file line by line and outputs their contents.

Step 2: Combining content

Listing both files separated by a comma in Get-Content merges their lines in order.

Step 3: Writing merged file

The combined output is sent to Set-Content which writes all lines into the new file.

🔄

Alternative Approaches

Using Add-Content
powershell
Get-Content file1.txt | Set-Content merged.txt
Get-Content file2.txt | Add-Content merged.txt
Write-Output "Files merged into merged.txt"
This method writes the first file then appends the second, useful if you want to merge files step-by-step.
Using Out-File with -Append
powershell
Get-Content file1.txt | Out-File merged.txt
Get-Content file2.txt | Out-File merged.txt -Append
Write-Output "Files merged into merged.txt"
Similar to Add-Content but uses Out-File, which allows more output formatting options.

Complexity: O(n) time, O(n) space

Time Complexity

Reading both files line by line and writing combined content takes time proportional to total lines, so O(n).

Space Complexity

The script holds all lines in memory before writing, so space is O(n) where n is total lines.

Which Approach is Fastest?

Using Get-Content with comma-separated files is simplest and efficient; appending methods add overhead but allow incremental writes.

ApproachTimeSpaceBest For
Get-Content with commaO(n)O(n)Simple, all-at-once merging
Set-Content + Add-ContentO(n)O(n)Stepwise merging, appending
Out-File with -AppendO(n)O(n)Formatted output, appending
💡
Ensure the output file does not exist or is okay to overwrite before merging files.
⚠️
Forgetting to separate file names with a comma in Get-Content causes errors or incomplete merging.