TEXT

Sunday, December 27, 2020

Compare two CSV files via PowerShell to find out difference and export results into new CSV file.

Compare two CSV files via PowerShell to find out difference and export results into new CSV file.

If you are looking for simple script can read two CSV file, which each CSV file has multiple columns, then compare them to find out the difference this script is just going to do that. Simply download and adjust few variables to make it work for your scenario.

I have added both CSV file content into script for you to see what script is pulling then variables you can change to make it fit into your needs.

Adjust your CSV file, if your columns are different, you have more columns etc. that is fine, just adjust them accordingly.

“Computername, and UserName “ are column name on the example below, thus script reflect both variables, and also PS custom object, just adjust them.

 Enjoy it.

<#

 

#$File1

 

ComputerName UserName

------------ --------

XLT-001      Allison1

XLT-002      Allison2

XLT-003      Allison3

XLT-004      Allison4

XLT-005      Allison5

XLT-006      Allison6

XLT-007      Allison7

XLT-008      Allison8

XLT-009      Allison9

XLT-010      Allison10

WRK-010      Clint

 

#$File2

ComputerName UserName

------------ --------

XLT-001      Allison1

XLT-002      Allison2

XLT-003      Allison3

XLT-004      Allison4

XLT-005      Allison5

XLT-006      Allison6

XLT-007      Allison7

WRK-001      RAMBO   

XLT-008      Allison8

XLT-009      Allison9

XLT-010      Allison10

 

 

#>

 

 

 

#(1)_.Path for CSV Files that needs to be comppared

$CSV1 = "C:\Users\12024\OneDrive - wiretecpro\Desktop\Compare-CSV\Excell\File1.csv"

$CSV2 = "C:\Users\12024\OneDrive - wiretecpro\Desktop\Compare-CSV\Excell\File2.csv"

 

 

 

#(2)_.Path for export results to CSV

$CSV3 = "C:\Users\12024\OneDrive - wiretecpro\Desktop\Compare-CSV\Excell\DIFFERENCE.csv"

 

 

 

#(3)_.Import both CSV Files you would like to compare

$File1 = Import-Csv $CSV1

$File2 = Import-Csv $CSV2

 

 

#(4)_.Compare both CSV files - column ComputerName,UserName

 

$Results = Compare-Object  $File1 $File2 -Property ComputerName,UserName -IncludeEqual

 

 #(5)_.Set Array

$Array = @()

 

#(6)_.Loop Starts     

Foreach($item in $Results)

{

    If( $item.sideindicator -notcontains "==" )

    {

        $Object = [pscustomobject][ordered] @{

            ComputerName      = $item.ComputerName

            UserName          = $item.UserName

            Compare_Indicator = $item.sideindicator

 

        }

        $Array += $Object

    }

}

 

#(7)_.Count users in both files

($Array | sort-object ComputerName | Select-Object * -Unique).count

 

#(8)_.Display results in console

$Array

 

#(9)_.Export results into CSV File

$Array | export-csv -Path $CSV3 -NoTypeInformation

 

 

Casey, Dedeal

Principal Systems Engineer

https://simplepowershell.blogspot.com/  (blog)

https://smtp25.blogspot.com/ (blog)

https://telnet25.wordpress.com/ (blog)

https://twitter.com/Message_Talk (Twitter)

 


No comments:

Post a Comment

Simple Function Do Something

   Use template below to create simple PS function    # Simple Function Do Something # Change $ReportName   function Fu...