Link to home
Start Free TrialLog in
Avatar of MRS
MRSFlag for United States of America

asked on

Powershell script to compare two txt files and create 3rd txt file with differences

I have two .txt files. One is with up to date 2 different NIC drivers brand and up to date versions that I will maintain manually:
DriverDesc     : Realtek PCIe GBE Family Controller
DriverDate     : 6-18-2013
DriverVersion  : 7.73.618.2013

DriverDesc     : Intel(R) 82579LM Gigabit Network Connection
DriverDate     : 5-15-2013
DriverVersion  : 12.6.45.0


and one .txt file with columns like:


PSComputerName : PC1
DriverDesc     : Intel(R) 82579LM Gigabit Network Connection
DriverDate     : 5-15-2013
DriverVersion  : 12.6.45.0

PSComputerName : PC2
DriverDesc     : Realtek PCIe GBE Family Controller
DriverDate     : 6-18-2013
DriverVersion  : 7.73.618.2013

PSComputerName : PC3
DriverDesc     : Realtek PCIe GBE Family Controller
DriverDate     : 6-18-2013
DriverVersion  : 7.73.618.2013

PSComputerName : PC4
DriverDesc     : Intel(R) 82579LM Gigabit Network Connection
DriverDate     : 10-31-2012
DriverVersion  : 12.2.45.0

PSComputerName : PC5
DriverDesc     : Intel(R) 82579LM Gigabit Network Connection
DriverDate     : 5-15-2013
DriverVersion  : 12.6.45.0

How do I create a script in PowerShell that will match "DriverDesc" between two .txt files, when it finds the match, compare "DriverVersion" field. If "DriverVerison" matches between two proceed with next one, if it doesn't grab the "PSComputerName" and below fields (DriverDesc, DriverDate, DriverVersion) and create separate (3rd)  .txt file that will have all the fields like second .txt file but just the PC's that are  not up to date with NIC drivers?
Avatar of footech
footech
Flag of United States of America image

If your text files are .CSVs then it's not too hard.  I'm attaching the files in that format.
$driverComp =  (Import-CSV diff.csv)
@(foreach ($ref in (Import-CSV ref.csv))
{
    foreach ($diff in $driverComp)
    {
        If ($ref.DriverDesc -eq $diff.DriverDesc)
        {
            If ($ref.DriverVersion -ne $diff.DriverVersion)
            { $diff }
        }
    }
}) | Export-CSV OutofDate.csv -notype

Open in new window

diff.csv
ref.csv
ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MRS

ASKER

You rock! Thanks.