'Concatenate files using PowerShell

I am using PowerShell 3.

What is best practice for concatenating files?

file1.txt + file2.txt = file3.txt

Does PowerShell provide a facility for performing this operation directly? Or do I need each file's contents be loaded into local variables?



Solution 1:[1]

If all the files exist in the same directory and can be matched by a simple pattern, the following code will combine all files into one.

Get-Content .\File?.txt | Out-File .\Combined.txt

Solution 2:[2]

I would go this route:

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

Use the -Encoding parameter on Set-Content if you need something other than ASCII which is the default for Set-Content.

Solution 3:[3]

If you need more flexibility, you could use something like

Get-ChildItem -Recurse *.cs | ForEach-Object { Get-Content $_ } | Out-File -Path .\all.txt

Solution 4:[4]

Warning: Concatenation using a simple Get-Content (whether or not using -Raw flag) works for text files; Powershell is too helpful for that:

  • Without -Raw, it "fixes" (i.e. breaks, pun intended) line breaks, or what Powershell thinks is a line break.
  • With -Raw, you get a terminating line end (normally CR+LF) at the end of each file part, which is added at the end of the pipeline. There's an option for that in newer Powershells' Set-Content.

To concatenate a binary file (that is, an arbitrary file that was split for some reason and needs to be put together again), use either this:

Get-Content -Raw file1, file2 | Set-Content -NoNewline destination

or something like this:

Get-Content file1 -Encoding Byte -Raw | Set-Content destination -Encoding Byte
Get-Content file2 -Encoding Byte -Raw | Add-Content destination -Encoding Byte

An alternative is to use the CMD shell and use

copy file1 /b + file2 /b + file3 /b + ... destinationfile

You must not overwrite any part, that is, use any of the parts as destination. The destination file must be different from any of the parts. Otherwise you're up for a surprise and must find a backup copy of the file part.

Solution 5:[5]

a generalization based on @Keith answer:

gc <some regex expression> | sc output

Solution 6:[6]

Here is an interesting example of how to make a zip-in-image file based on Powershell 7

Get-Content -AsByteStream file1.png, file2.7z | Set-Content -AsByteStream file3.png
Get-Content -AsByteStream file1.png, file2.7z | Add-Content -AsByteStream file3.png

Solution 7:[7]

gc file1.txt, file2.txt > output.txt

I think this is as short as it gets.

Solution 8:[8]

In case you would like to ensure the concatenation is done in a specific order, use the Sort-Object -Property <Some Name> argument. For example, concatenate based on the name sorting in an ascending order:

Get-ChildItem -Path ./* -Include *.txt -Exclude output.txt | Sort-Object -Property Name | ForEach-Object { Get-Content $_ } | Out-File output.txt

IMPORTANT: -Exclude and Out-File MUST contain the same values, otherwise, it will recursively keep on adding to output.txt until your disk is full.

Note that you must append a * at the end of the -Path argument because you are using -Include, as mentioned in Get-ChildItem documentation.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 nabrond
Solution 2 Keith Hill
Solution 3 Dan Friedman
Solution 4
Solution 5 yaitloutou
Solution 6 SekiBetu
Solution 7 Shadow Man
Solution 8 M. Al Jumaily