# Compare & Add Content

**URL:** <https://forums.powershell.org/t/compare-add-content/420>\
**Category:** PowerShell Help\
**Created:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/compare-add-content/420 "2011-12-31T18:30:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![archives](https://avatars.discourse-cdn.com/v4/letter/a/bb73d2/32.png) [@archives](https://forums.powershell.org/u/archives)\
**Post date:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/compare-add-content/420/1 "2011-12-31T18:30:00Z")

</div>

by ChrisL at 2012-12-18 11:58:19

> Ok, so I have a script in which we are adding the contents of one file to another (g-c | a-c). I’m trying to modify it to compare the files line by line and only add what’s in the second file that isn’t contained in the first. My firs stab is the following, which filters out the differences between the files and then adds the result to a temp file. Repeat to pipe the results of the first Compare Object into a second one where I compare against the local file again.  
>   
> `	Compare-Object $(Get-Content "$sLocalStore$sEnvConfig\file1.ini") $(Get-Content "$sProd\file2.ini") | Select-Object -ExpandProperty InputObject | Add-Content "$sProd\tmp.ini" -Force Compare-Object $(Get-Content "$sProd\tmp.ini") $(Get-Content "$sProd\File2.ini") | Select-Object -ExpandProperty InputObject | Add-Content "$sProd\File2.ini" -Force del "$sProd\tmp.ini" -Force `  
>   
> The issue is there’s still an entry that’s being duplicated, even though it’s even in the same position in each file. I’m also wondering is is there a way to pull this off without the temp file; I’m sure there is, I’m just not quite there yet. Any ideas would be appreciated, thanks 🙂

by nohandle at 2012-12-18 14:59:48

> this should probably work:  
> `$copy = 'c:\temp\compare\copy.txt'$orig = 'c:\temp\compare\orig.txt'Compare-Object -ReferenceObject (get-Content $orig) -DifferenceObject (get-Content $copy) | where {$_.sideIndicator -eq "<="} | select -ExpandProperty InputObject | Add-Content $copy`

by ChrisL at 2012-12-19 13:11:13

> Perfect! Thank you so much!

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 9:04pm UTC](https://forums.powershell.org/t/compare-add-content/420/2 "2024-05-16T21:04:32Z")

</div>


