I have two csv files i need to compare them, then output a new file that will have the new values of the prophetess that changes and will mark(highlight) those properties some how. I already have the resulting file with all the values and all the properties but i dont know how to mark the specific properties that changed. Is there a way to do that is powershell?
Here is a sample of my code:
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
$file1,
[Parameter(Mandatory=$true)]
$file2
)
$content1 = Import-Csv -Path $file1
$content2 = Import-Csv -Path $file2
$props = $content1 | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty Name
$comparedLines = Compare-Object $content1 $content2 -Property $props -PassThru | Where-Object {$_.SideIndicator -eq "=>" }
$comparedLines | Export-csv -Path C:\FullPUF\Difference.csv –NoTypeInformation
So, if I understand this correctly,
$content2
is the newer file, right?If so, then
$comparedlines
is going to contain all of the values which changed. If that's right, then we're in business, because Doug Finke wrote an absolutely awesome PowerShell module called importExcel that is going to make this trivially easy. But first, how did I do this?I needed some files to test against, and I didn't have your source file so I couldn't guess at the values. I just made a simple CSV with a Name and Position value, and changed the Position numbers between Content1 and Content2.
How to solve this problem, first off, download Import-Excel right now. If you're on PowerShell version 4.0 or higher you can install it from PowerShell like so:
This module has the concept of ConditionalText filter rules. You can create a new one like this.
This will output and display a new spreadsheet with the numbers 1 through 5 in it, and highlight the ones higher than 3.
We can also highlight rows that ContainsText which matches a value we know.
So, we take the value of $comparedLines using ForEach-Object, and create a New-ConditionalText rule to highlight the line if it contains one of the properties.
Next, we will echo the contents of $content2, and use Export-Excel to create a new .xslx file and apply a conditional formatting rule to it.
And the output
You'll probably need to play with the code a tiny bit, and modify it to fit whatever your columns are called, but this is how you can export a csv and highlight the differences using PowerShell. Except there's no such thing as highlighting or text format in a .csv so you have to use an Excel SpreadSheet instead.
Lemme know if you need me to dig deeper anywhere :)