I would like to remove duplicates in a CSV file using PowerShell. I know that there are posts about this already but I can't seem to find one that helps.
I'm trying to merge 2 CSV Files that have the same header and then remove the duplicates of the resulting file based on the IDs listed in the first column and then put it to the same CSV file.
The properties of the file are as follows:

And when I try to use the sort and unique method, I get the following (not a table:

####
#MERGE
$getFirstLine = $true
get-childItem "C:\IGHandover\Raw\IG_INC*.csv"| foreach {
$filePath = $_
$lines = $lines = Get-Content $filePath
$linesToWrite = switch($getFirstLine) {
$true {$lines}
$false {$lines | Select -Skip 1}
}
$getFirstLine = $false
Add-Content "C:\IGHandover\new.csv" $linesToWrite
}
####
#REMOVE DUPLICATES
Import-Csv "C:\IGHandover\new.csv" | Sort inc_number -Unique |
Set-Content "C:\IGHandover\new.csv"

Import-Csv "C:\IGHandover\new.csv" | Sort inc_number -Uniqueto display data in tabular format thenFormat-Table -AutoSizeis what you are looking for. But that is just for your representation purpose on the Shell screen. What is it exactly you are looking for? Do theSortandUniqueproperties don't work correctly for you?Set-ContentioExport-Csv -NoClobber. Solving that takes you to another hurdle: you are writing to a file you are still reading from. That can be solved by adding brackets around the import but much easier is to write to a new file.