locked
Windows PowerShell Compare 2 Text Files and Output the difference. RRS feed

  • Question

  • I have been working on a script to compare 2 text files and output the difference into a third text file.Text file 1 contains all applications from a fresh install of the windows 7 OS. Text file 2 contains all applications the current user has installed. text file 3 should only contain the differences between text file 1 and 2.

    example:

    Text File1 Text File 2
    Adobe Acrobat Reader DC                                                                                                 64 Bit HP CIO Components Installer                                                                                                                                                                            
    Adobe Flash Player 28 ActiveX                                                                                           Adobe Acrobat Reader DC                                                                                                                                                                                       
    Adobe Refresh Manager                                                                                                   Adobe Refresh Manager                                                                                                                                                                                         
    Check Point VPN                                                                                                         Bonjour                                                                                                                                                                                                       
    Cisco AnyConnect Secure Mobility Client                                                                                 Check Point VPN                                                                                                                                                                                               
    Google Chrome                                                                                                           Cisco AnyConnect Secure Mobility Client                                                                                                                                                                       
    Google Update Helper                                                                                                    Desktop Authority Computer Agent                                                                                                                                                                              
    IBM Notes 9.0.1 Social Edition                                                                                          Dolby Audio X2 Windows API SDK                                                                                                                                                                                
    IBM WebSphere MQ                                                                                                        FileMaker Pro 15                                                                                                                                                                                              
    IBM Websphere MQ 64bitSupport                                                                                           Google Chrome                                                                                                                                                                                                 
    Java 8 Update 161 (64-bit)                                                                                              Google Update Helper                                                                                                                                                                                          
    Java Auto Updater                                                                                                       GoToMyPC                                                                                                                                                                                                      
    Microsoft .NET Framework 4.7.2 GoToMyPC Print Assistant                                                                                                                                                                                      

                                                                                                                                                     

    Wednesday, July 31, 2019 7:05 PM

Answers

  • And, of course, with objects we do not need to select and sort before comparing.

    Get-WmiObject -Class Win32_Product | Export-CliXml prod1.clixml
    $oldprod = Import-Clixml prod1.clixml
    $newprod = Get-WmiObject -Class Win32_Product
    Compare-Object $oldprod $newprod -Property Name -IncludeEqual | sort name
    

    With PowerShell always use objects over text whenever possible.  PowerShell and Net are "object" systems and NOT text systems.


    \_(ツ)_/

    Thursday, August 8, 2019 2:19 PM
  • This will provide the difference with arrows of which file has the that info. 

    Compare-Object -ReferenceObject $(Get-Content F:\file1.txt) -DifferenceObject $(Get-Content F:\file2.txt) | ft | out-file F:\file3.txt

    Wednesday, July 31, 2019 8:34 PM

All replies

  • You should show your code as well!!

    Take a look at the cmdlet Compare-Object.


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Wednesday, July 31, 2019 8:16 PM
  • This will provide the difference with arrows of which file has the that info. 

    Compare-Object -ReferenceObject $(Get-Content F:\file1.txt) -DifferenceObject $(Get-Content F:\file2.txt) | ft | out-file F:\file3.txt

    Wednesday, July 31, 2019 8:34 PM
  • Please format your code as code using the code posting tool provided on the icon bar of the post editor (second to last icon).

    Regardless of that - you should make your code as descriptive as possible. Try to keep in mind that you help people less experienced than you ... probably beginners ...


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Wednesday, July 31, 2019 11:05 PM
  • Hi,

    Thanks for your question.

    text file 3 should only contain the differences between text file 1 and 2.

    Please try the code below:

    $result=Compare-Object -ReferenceObject (Get-Content C:\test\file1.txt) -DifferenceObject (Get-Content C:\test\file2.txt) 
    $result.inputobject | out-file C:\test\file3.txt

    If you want to know more about it, please refer the link below:

    https://dotnet-helpers.com/powershell/compare-two-files-list-differences/

    https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/compare-object?view=powershell-6

    Best regards,

    Lee


    Just do it.

    Thursday, August 1, 2019 3:27 AM
  • This is a copy of my code

    Clear-Host # Clear Screen
    $COMPUTERNAME = hostname # Get Host Computer name
    Get-WmiObject -Class Win32_Product | Sort-Object -Property Name | FT Name > E:\Script\LICAPPFILE.txt 
    $STDIMGFILE = Get-Content E:\Script\STDIMGFILE.txt  #Text File1
    $LICAPPFILE = Get-Content E:\Script\LICAPPFILE.txt  #Text File 2
    Get-Content $STDIMGFILE | ForEach-Object {$_.split(",")[1]} > E:\Script\SORT-STDIMGFILE.txt
    Get-Content $LICAPPFILE | ForEach-Object {$_.split(",")[1]} > E:\Script\SORT-LICAPPFILE.txt
    $SORTSTDIMGFILE = E:\Script\SORT-STDIMGFILE.txt
    $SORTLICAPPFILE = E:\Script\SORT-LICAPPFILE.txt  
    Compare-object   -ReferenceObject $SORTSTDIMGFILE -DifferenceObject $SORTLICAPPFILE -includeequal >E:\Script\$COMPUTERNAME-ONSTDAPPS.txt  #Text File 3
    Write-host "Completed"





    Thursday, August 1, 2019 5:00 PM
  • Please format your code as code using the code posting tool provided on the icon bar of the post editor (second to last icon).

    Regardless of that - it is not necessary to comment each and every single line of code. Powershell code is quite self-explanatory.

    You might read The PowerShell Best Practices and Style Guide.


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Thursday, August 1, 2019 10:37 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Monday, August 5, 2019 9:55 AM
  • The issue hasn't been resolved as yet. I tried the code you suggested and got the same output.

    here is my modified code

    Clear-Host # Clear Screen
    $COMPUTERNAME = hostname # Get Host Computer name
    Get-WmiObject -Class Win32_Product | Sort-Object -Property Name | FT Name > E:\Script\LICAPPFILE.txt #Search and filter for all host computer application. Write output to a text file name LICAPPFILE.txt.
    
    $STDIMGFILE = Get-Content E:\Script\STDIMGFILE.txt #Read STDIMGFILE.txt file content
    $LICAPPFILE = Get-Content E:\Script\LICAPPFILE.txt #Read LICAPPFILE.txt file content
    
    Compare-object  -ReferenceObject $STDIMGFILE  -DifferenceObject $LICAPPFILE -includeequal >E:\Script\$COMPUTERNAME-NONSTDAPPS.txt # Compare STDIMGFILE.txt and LICAPPFILE.txt and out only the difference to $COMPUTERNAME-NONSTDAPPS.txt.   
    Write-host "Completed"


    Thursday, August 8, 2019 1:54 PM
  • To compare text files you need to use a txt file comparison utility.

    Windows has always had "comp".

    comp /?

    comp E:\Script\STDIMGFILE.txt E:\Script\LICAPPFILE.txt

    PowerShell uses objects.

    To compare WMI objects you will need to select the identifier and compare the identifiers.

    You can save objects in object format.

    See:

    help export-clixml -online

    help import-clixml -online


    \_(ツ)_/

    Thursday, August 8, 2019 2:08 PM
  • Example:

    Get-WmiObject -Class Win32_Product | Select Name | Sort-Object Name | Export-CliXml prod1.clixml
    $oldprod = Import-Clixml prod1.clixml
    $newprod = Get-WmiObject -Class Win32_Product | Select Name | Sort-Object Name
    Compare-Object $oldprod $newprod -Property Name -IncludeEqual


    \_(ツ)_/


    • Edited by jrv Thursday, August 8, 2019 2:19 PM
    Thursday, August 8, 2019 2:13 PM
  • And, of course, with objects we do not need to select and sort before comparing.

    Get-WmiObject -Class Win32_Product | Export-CliXml prod1.clixml
    $oldprod = Import-Clixml prod1.clixml
    $newprod = Get-WmiObject -Class Win32_Product
    Compare-Object $oldprod $newprod -Property Name -IncludeEqual | sort name
    

    With PowerShell always use objects over text whenever possible.  PowerShell and Net are "object" systems and NOT text systems.


    \_(ツ)_/

    Thursday, August 8, 2019 2:19 PM
  • This would be the correct way to compare products.

    Compare-Object $oldprod $newprod -Property IdentifyingNumber,Name,Version -IncludeEqual | sort name


    \_(ツ)_/

    • Proposed as answer by BOfH-666 Thursday, August 8, 2019 9:03 PM
    Thursday, August 8, 2019 2:24 PM
  • Hello Thanks for your hello thus far. Your scripting Technics did help alot but i realized that the Get-WmiObject -Class Win32_product doesnt capture all the applications listed in control panel program features. I attempted to use the the following example in the link below to obtain a list of software but didnt had much success. https://gallery.technet.microsoft.com/scriptcenter/PowerShell-Installed-70d0c0f4

    My code looks  

    Clear-Host # Clear Screen
    $COMPUTERNAME = hostname # Get Host Computer name
    Get-WmiObject -Class Win32_Product |Select Name| Sort-Object -Property Name| Export-CliXml E:\Script\LICAPPFILE.clixml #Search and filter for all host computer application. Write output to a text file name LICAPPFILE.txt.
    
    $STDIMGFILE = import-clixml E:\Script\STDIMGFILE.clixml #Read STDIMGFILE.txt file content
    $LICAPPFILE = import-clixml E:\Script\LICAPPFILE.clixml #Read LICAPPFILE.txt file content
    
    Compare-object  -ReferenceObject $STDIMGFILE  $LICAPPFILE -Property Name| sort name | Out-File E:\Script\$COMPUTERNAME-NONSTDAPPS.clixml # Compare STDIMGFILE.txt and LICAPPFILE.txt and out only the difference to $COMPUTERNAME-NONSTDAPPS.txt.   
    Write-host "Completed"



    Friday, August 9, 2019 10:15 PM
  • That is true,  Many programs are installed as part of other programs and are not part of a separate install.

    The control panel uses other methods to discover programs.  Many control panel items are system level installs such as the Net Framework.

    Also many online programs are not listed and Store apps are not listed.

    You also are using "Out-File" to a CLIXML file which will not work.  Out-File only creates text files.


    \_(ツ)_/


    • Edited by jrv Friday, August 9, 2019 10:55 PM
    Friday, August 9, 2019 10:52 PM
  • Also note that you code is not what I posted and will not work as required.


    \_(ツ)_/

    Friday, August 9, 2019 10:56 PM
  • Do you know what method the control panel uses to discover programs and can you create an example with powershell code?

    Iam trying to use the get-softwarelist command found on https://gallery.technet.microsoft.com/scriptcenter/PowerShell-Installed-70d0c0f4 but when i run it i get the following message. I click on the Run once and nothing happens after.  Is there something Iam doing wrong

    

    Monday, August 12, 2019 2:55 PM
  • Please take the time to red the instructions posted by the author of the script.  Also take some time to learn basic PowerShell.

    For questions about Gallery scripts post the authors Gallery page here: Q and A 

    Your original question has been answered.  If you have new questions please open a new topic.  


    \_(ツ)_/

    Monday, August 12, 2019 4:02 PM