How do I concatenate two text files in PowerShell?

PowershellMergeText FilesPowershell 2.0

Powershell Problem Overview


I am trying to replicate the functionality of the cat command in Unix.

I would like to avoid solutions where I explicitly read both files into variables, concatenate the variables together, and then write out the concatenated variable.

Powershell Solutions


Solution 1 - Powershell

Simply use the Get-Content and Set-Content cmdlets:

Get-Content inputFile1.txt, inputFile2.txt | Set-Content joinedFile.txt

You can concatenate more than two files with this style, too.

If the source files are named similarly, you can use wildcards:

Get-Content inputFile*.txt | Set-Content joinedFile.txt

Note 1: PowerShell 5 and older versions allowed this to be done more concisely using the aliases cat and sc for Get-Content and Set-Content respectively. However, these aliases are problematic because cat is a system command in *nix systems, and sc is a system command in Windows systems - therefore using them is not recommended, and in fact sc is no longer even defined as of PowerShell Core (v7). The PowerShell team recommends against using aliases in general.

Note 2: Be careful with wildcards - if you try to output to inputFiles.txt (or similar that matches the pattern), PowerShell will get into an infinite loop! (I just tested this.)

Note 3: Outputting to a file with > does not preserve character encoding! This is why using Set-Content is recommended.

Solution 2 - Powershell

Do not use >; it messes up the character encoding. Use:

Get-Content files.* | Set-Content newfile.file

Solution 3 - Powershell

In cmd, you can do this:

copy one.txt+two.txt+three.txt four.txt

In PowerShell this would be:

cmd /c copy one.txt+two.txt+three.txt four.txt

While the PowerShell way would be to use gc, the above will be pretty fast, especially for large files. And it can be used on on non-ASCII files too using the /B switch.

Solution 4 - Powershell

You could use the Add-Content cmdlet. Maybe it is a little faster than the other solutions, because I don't retrieve the content of the first file.

gc .\file2.txt| Add-Content -Path .\file1.txt

Solution 5 - Powershell

To concat files in command prompt it would be

type file1.txt file2.txt file3.txt > files.txt

PowerShell converts the type command to Get-Content, which means you will get an error when using the type command in PowerShell because the Get-Content command requires a comma separating the files. The same command in PowerShell would be

Get-Content file1.txt,file2.txt,file3.txt | Set-Content files.txt

Solution 6 - Powershell

If you need to order the files by specific parameter (e.g. date time):

gci *.log | sort LastWriteTime | % {$(Get-Content $_)} | Set-Content result.log

Solution 7 - Powershell

I used:

Get-Content c:\FileToAppend_*.log | Out-File -FilePath C:\DestinationFile.log 
-Encoding ASCII -Append

This appended fine. I added the ASCII encoding to remove the nul characters Notepad++ was showing without the explicit encoding.

Solution 8 - Powershell

To keep encoding and line endings:

Get-Content files.* -Raw | Set-Content newfile.file -NoNewline

Note: AFAIR, whose parameters aren't supported by old Powershells (<3? <4?)

Solution 9 - Powershell

You can do something like:

get-content input_file1 > output_file
get-content input_file2 >> output_file

Where > is an alias for "out-file", and >> is an alias for "out-file -append".

Solution 10 - Powershell

Since most of the other replies often get the formatting wrong (due to the piping), the safest thing to do is as follows:

add-content $YourMasterFile -value (get-content $SomeAdditionalFile)

I know you wanted to avoid reading the content of $SomeAdditionalFile into a variable, but in order to save for example your newline formatting i do not think there is proper way to do it without.

A workaround would be to loop through your $SomeAdditionalFile line by line and piping that into your $YourMasterFile. However this is overly resource intensive.

Solution 11 - Powershell

I think the "powershell way" could be :

set-content destination.log -value (get-content c:\FileToAppend_*.log )

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmerlin2011View Question on Stackoverflow
Solution 1 - PowershellSmiView Answer on Stackoverflow
Solution 2 - Powershelluser2074686View Answer on Stackoverflow
Solution 3 - PowershellmanojldsView Answer on Stackoverflow
Solution 4 - PowershellmjsrView Answer on Stackoverflow
Solution 5 - PowershellBrian KimballView Answer on Stackoverflow
Solution 6 - PowershellRoman OView Answer on Stackoverflow
Solution 7 - PowershellPhoenix14830View Answer on Stackoverflow
Solution 8 - PowershellIlyanView Answer on Stackoverflow
Solution 9 - Powershellvlad-ardeleanView Answer on Stackoverflow
Solution 10 - PowershellKamaradskiView Answer on Stackoverflow
Solution 11 - PowershelldvjzView Answer on Stackoverflow