In this article, we will discuss ways of concatenating multiple files into one single file using PowerShell.
We will be using these three files in our examples: file1.txt, file2.txt, and file2.txt with the following contents:
file1.txt
File1 Lorem Ipsum is simply dummy text File1 printing and typesetting industry. File1 Lorem Ipsum has been the industry's
file2.txt
File2 standard dummy text ever since the
file3.txt
File3 to make a type specimen book. File3 It has survived not only five centuries,
Method 1: Using Get-Content and Set-Content Commands
Get-Content Powershell cmdlet is used to get the content of item(s) at the specified location. Once we have the content of the files, we can use Set-Content to write new content or replaces existing content in a file.
Alternatively, we can use Add-Content if we want to append content to an existing file if we do not like its content replaced.
Concatenate file1.txt and file3.txt to get output1.txt
1 |
Get-Content -Path .\files\file1.txt, .\files\file3.txt | Set-Content -Path output1.txt |
The above command concatenates file1.txt and file3.txt to get output1.txt.
Verify that concatenation happened
We can verify that file1.txt and file3.txt were merged by checking the contents of the new output1.txt file using the Get-Content cmdlet.
1 |
Get-Content .\output1.txt |
If you want to get the contents of all files in a folder and concatenate them, then we can use a regular expression on the -Path parameter of Get-Content. For example, the following line will merge all txt files in the files folder into a new output2.txt file.
1 |
Get-Content -Path .\files\*.txt | Set-Content -Path output2.txt |
On the other hand, the following line will concatenate file1.txt and file3.txt and append the result to the output3.txt file (without overwriting). If output3.txt does not exist, it is created.
1 |
Get-Content -Path files/file1.txt, files/file3.txt | Add-Content -Path output3.txt |
You can also use the piping operators (> or >>) in place of Set-Content to send the output of Get-Content to a file. The “>” operator overwrites content while “>>” appends content without replacing the file. For example,
1 2 |
Get-ChildItem -Path files/ | Get-Content >> out.txt Get-ChildItem -Path files/ | Get-Content > out.txt |
Get-ChildItem generates a list of all files in -Path. This list is then passed to Get-Content, which reads the contents of the files and writes them to out.txt.
You can also use Out-File to send contents to a file.
1 |
Get-Content -Path files/file1.txt, files/file2.txt | Out-File -FilePath test3.txt |
Method 2: Using the cat Command
The cat command is primarily used to concatenate files. The following line merges file1.txt and file2.txt to ouputx.txt. If outputx.txt already exists, it is replaced. If you want to append content instead, use “>>” instead of > piping operator.
1 |
cat files/file1.txt, files/file2.txt > ouputx.txt |
You can also use regular expressions with the cat command. The following example merges all files in a files folder matching file?.txt pattern, that is, file1.txt, file2.txt, filea.txt, etc.
1 |
cat files/file?.txt > ouputx1.txt |
We can also use the cat command to check the content of outputx1.txt.
1 |
cat output1.txt |
Method 3: Using gc Command
The gc command works just like cat. The following command merges file1.txt and file2.txt to output.txt.
1 |
gc files/file1.txt, files/file2.txt > ouputy.txt |
And the following merge all .txt files in the files folder.
1 |
gc files/*.txt >outputy1.txt |
Note: cat and gc commands are aliases for Get-Content; they are used to accomplish the same purpose.
Method 4: Using Merge-PDF Command to Merge PDF Files
PSWritePDF module comes with the Merge-PDF command (https://github.com/EvotecIT/PSWritePDF).
Note (Important!): You may need to run PowerShell as Administrator to install or update PSWritePDF and to use the Merge-PDF command. You may also find it convenient to provide an absolute path for the output.
To use it, we need to install it first. You can do that by running the following command.
1 |
Install-Module PSWritePDF -Force |
And to update it, use this command:
1 |
Update-Module PSWritePDF |
Then use the Merge-PDF command with something like this:
1 |
Merge-PDF -InputFile .\receipt1.pdf, .\receipt2.pdf -OutputFile C:\Users\kipro\OneDrive\Desktop\output.pdf |
Once again, remember to run PowerShell as admin when running all commands in this method and provide an absolute path (not relative) on the -OutputFile parameter.
Conclusion
This article discussed PowerShell commands to concatenate text and PDF files. To merge text files, use Get-Content + Set-Content, cat, or gc commands. These commands come preinstalled in Windows. For merging PDFs, you need to install the PSWritePDF module, which comes with the Merge-PDF command that can be conveniently used to merge PDF files.