Powershell: script modification


I am trying to compare 2 files, and output a file containing the matches, I have the following working
Compare-Object -ReferenceObject $(Get-Content filea.txt) -DifferenceObject $(Get-Content fileb.txt) -IncludeEqual  | where-object {$_.SideIndicator -eq "=="} | Out-File c:\match.txt

How do I output the differences to a file.

Thanks for your help.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Parity123Author Commented:
I want to list only the contents in Fileb that are not in FileA
Parity123Author Commented:
I tried this and it worked.

Compare-Object -ReferenceObject $(Get-Content filea.txt) -DifferenceObject $(Get-Content fileb.txt) -IncludeEqual  | where-object {$_.SideIndicator -eq "=>"} | Out-File c:\differences.txt

unless there is a better way to out the match and difference in the same statement
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Compare-Object (Get-Content filea.txt) (Get-Content fileb.txt) |
  where-object {$_.SideIndicator -eq "=>"} | 
  Select-Object  -Expand InputObject |
  Out-File c:\differences.txt

Open in new window

In particular the -IncludeEqual is unnecessary, as it includes all identical lines, which then need to get filtered out.
The Select-Object -Expand will only output the line as a string (not object) without the side indicator.
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

Parity123Author Commented:
If I want to output the matches and differences in 2 different files called match.txt and difference.txt, is it possible do it in the same loop you had indicated
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
That is more complex. I'll show a complete solution, with left, right and both:
$results = Compare-Object (Get-Content filea.txt) (Get-Content fileb.txt) -IncludeEqual
$results | where-object {$_.SideIndicator -eq "=>"} | Select-Object  -Expand InputObject | Out-File c:\RightOnly.txt
$results | where-object {$_.SideIndicator -eq "<="} | Select-Object  -Expand InputObject | Out-File c:\LeftOnly.txt
$results | where-object {$_.SideIndicator -eq "=="} | Select-Object  -Expand InputObject | Out-File c:\match.txt

Open in new window

But that can get inefficient if you have large files, because of the added overhead.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Parity123Author Commented:
Thank you
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.