Merging multiple text files into one in PowerShell can be accomplished using the Get-Content and Set-Content cmdlets in PowerShell.
Concatenating files into one is necessary for several reasons, including data consolidation, and simplification of file management to combine files into one file to reduce the number of files that need to be stored.
The Get-Content cmdlet in PowerShell gets the content of the file and using the Set-Content, it writes the content in a file.
In this article, we will discuss how to use the Get-Content and Set-Content cmdlets in PowerShell to merge multiple text files into one file.
PowerShell Concatenate files into One
To compile multiple text files into one file, use the Get-Content cmdlet that read the content of the text files and passes the content to the Set-Content cmdlet which writes the output to the single file.
Let’s consider, the Logs folder contains three files Log1.txt, Log2.txt, and Log3.txt. These files contain the event log in them.
# Get the items from the folder Get-ChildItem -Path D:\Logs\ # Get the content of Log1.txt file1 Get-Content -Path D:\Logs\Log1.txt # Get the content of Log2.txt file2 Get-Content -Path D:\Logs\Log2.txt # Get the content of Log3.txt file3 Get-Content -Path D:\Logs\Log3.txt
In the above PowerShell script, the Get-ChildItem cmdlet in PowerShell gets the items from the specified folder path. Using the Get-Content command, it gets the content of the file and displays it on the terminal.
To merge multiple files into one file, like ( Log1.txt + Log2.txt + Log3.txt), run the following PowerShell script that includes the Get-Content and Set-Content cmdlets.
# Concatenate multiple files using the Get-Content and write to single file Get-Content -Path D:\Logs\*.txt | Set-Content -Path D:\Logs\Log_merge.txt # Get the content of merged file Get-Content -Path D:\Logs\Log_merge.txt
In the above PowerShell script, the Get-Content cmdlet uses the Path
parameter to get the content of all .txt
files from the specified directory path, merge them together and pass them to the Set-Content to write the output to one file.
The output of the combination of multiple files into one file contains content from all files available in the folder.
To concatenate multiple files of different extensions like *.txt,*.log, etc.., run the following command.
# Concatenate multiple files using the Get-Content and write to single file Get-Content -Path D:\Logs\*.txt, D:\Logs\*.log | Set-Content -Path D:\Logs\Log_merge.txt
In the above PowerShell script, the Get-Content gets the content from the multiple file paths, reads the contents of .txt
and .log
file, and combine them into a single string. The Set-Content command uses the content of the combined files and writes to one file.
Cool Tip: How to compare two strings in PowerShell!
Conclusion
I hope the above article to use the Get-Content and Set-Content cmdlets in PowerShell to merge multiple text files into one file in PowerShell is helpful to you.
You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.