Categories

A sample text widget

Etiam pulvinar consectetur dolor sed malesuada. Ut convallis euismod dolor nec pretium. Nunc ut tristique massa.

Nam sodales mi vitae dolor ullamcorper et vulputate enim accumsan. Morbi orci magna, tincidunt vitae molestie nec, molestie at mi. Nulla nulla lorem, suscipit in posuere in, interdum non magna.

Combine or Join Two Text Files Using PowerShell

Merging files together, or to a seperate file, is a snap with PowerShell using the Get-Content cmdlet. If you’ve never used the Add-Content cmdlet, I would suggest first reading my other article Append Text to a File Using Add-Content. The first thing we need to do is identify our two files, in my case I’ll be using file1.log and file2.log. In the first example, I append all the text of file2.log into the end of file1.log. In the second example, I’ll create a completely new file and dump the contents of both the files into the newly created file. In the last example, I’ll add the current date to the filename of a completly new log file.

1
2
$file2 = Get-Content "file2.log"
Add-Content "file1.log" $file2

Now, what if we want to copy the contents of file1.log and file2.log into a completely new file called file3.log

1
2
3
4
5
New-Item -ItemType file "file3.log"
$file1 = Get-Content "file1.log"
$file2 = Get-Content "file2.log"
Add-Content "file3.log" $file1
Add-Content "file3.log" $file2

What if we want to erase everything in file3.log before we add new content? In this case, instead of creating a new file, we’ll use the Clear-Contents cmdlet.

1
2
3
4
5
Clear-Content "file3.log"
$file1 = Get-Content "file1.log"
$file2 = Get-Content "file2.log"
Add-Content "file3.log" $file1
Add-Content "file3.log" $file2

Ok, well, what if I don’t know if file3.log already exists? If it doesn’t exist and I run the Clear-Contents cmdlet then I’ll get an error. And if it does exist and I try to create it as a new file I’ll still get an error. There’s an easy way to do this, we’ll use the New-Item cmdlet from the example above, but we’ll add the -force argument, so even if the file exists it will overwrite it.

1
2
3
4
5
New-Item -ItemType file "file3.log" -force
$file1 = Get-Content "file1.log"
$file2 = Get-Content "file2.log"
Add-Content "file3.log" $file1
Add-Content "file3.log" $file2

Alright, so I guess I got the hang of this, now lastly since these are log files, how can I append the date to the file name? Simple, with the Get-Date cmdlet. Let’s output our file name as YYYYMMDD-WebAccess.log in this example.

1
2
3
4
5
6
7
$date= (Get-Date).ToString("yyyyMMdd")
$NewFileName = "$date-WebAccess.log"
New-Item -ItemType file $NewFileName
$file1 = Get-Content "file1.log"
$file2 = Get-Content "file2.log"
Add-Content $NewFileName $file1
Add-Content $NewFileName $file2

4 comments to Combine or Join Two Text Files Using PowerShell

  • Great Tip, Gerardo.
    May i add this tip to our Powershelltips Database ?

    Regards,
    Bernard Flach
    Powershelltips.org

  • Squashman

    Does this stream the content to the output file? I would think that would be really slow if it did. At least in batch it is really slow when using a for loop or type to parse a file and append it to the end of another.

    I currently have a batch file that ultimately does this.
    copy /b file1 + file2 + file3 filescombined

    I am kind of surprised that powershell doesn’t have a similar command to copy and combine files together. Well not really. Everything with powershell is twice as much code most of the time.

  • awender

    It’s different, because you are expected to solve a problem like concatenating 20.000+ files with changing file names without a problem. Try to do it with your batch file….

  • TedA

    Thanks for posting this. I’m a novice to Powershell and this was very helpful. Thanks so very much.

Leave a Reply