Powershell: script modification

Hello,

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.
Parity123Asked:
Who is Participating?
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
0
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
0
QlemoBatchelor, 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.
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

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
0
QlemoBatchelor, 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.
0

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
0
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
Powershell

From novice to tech pro — start learning today.