If command issue

I have been trying to compare 2 arrays with the if command and I am having trouble understanding where I am going wrong, I have been using the ForEach to search each line which as far as I can tell is loading properly and then using an if statement I have been using the Where-Object to compare the second array and I do not think the data is coming out right.

Sample
If (!($file2 | Select-Object -Unique {$_.ID -ne $line.ID})

What is the best way to display the output of the above sample (especially the $_.ID) so I can check to make sure it is working it correctly.

Thanks

Lyn, the result of an if statement is either $true or $false. So it does not make sense to compare arrays with it. You would have to extract each item from each array and compare it to every single item of the other array. But with Compare-Object Powershell does the trick for you as I tried to show for you in your other thread.

Hi Olaf,

I understand that one way is to use the compare-object although I did get the Where-object to work with a lot of reading and experimenting. I do appreciate your advice and help.

Below is the final script that worked for me:

Comparing 2 CSV files and exporting difference to seperate a file

$file1 = Import-Csv “.\SAS1.csv” # SAS file
$file2 = Import-Csv “.\Acer.csv” # Acer File

$arrResult = @()

ForEach ($line in $file1)

{
If (!($file2 | Where-Object {$_.‘Unique ID’ -eq $line.‘Unique ID’}))

 {
    $arrResult += $line
  }

}

$arrResult | Export-Csv “.\NewStudents.csv” -NoTypeInformation

Thanks again for your help Olaf

very simple.

$arr1 = 1…10
$arr2 = 1…15

$arr2 | ? {$_ -notin $arr1}

@Dan

that only gives you the differences from array 2. If you would like to have the differences from both arrays you would have to do it also wise versa, right?

Like this:

$arr1 = 1..10
$arr2 = 5..15
($arr1 | ? {$_ -notin $arr2}) + ($arr2 | ? {$_ -notin $arr1})

(compare $arr1 $arr2).inputobject

that should make it completely clear for Lyn.
Thanks