Compare-object with csv files

i have a script to gather computer information
$cpus = get-ciminstance -classname Win32_Processor |select-object -Property DeviceID, Name |Export-Csv -Path $cpusfile
$ccpus = Import-Csv -Path $cpusfile
$pcpus = Import-Csv -Path $pcpusfile # this is a copy of the same file from the previous check
$cpuchanges = Compare-Object -ReferenceObject $pcpus -DifferenceObject $cpus -PassThru | Where-Object{$.SideIndicator -eq ‘=>’}
it comes back empty
if i export to txt files and do the same is works. i verified that the variables contain the correct data but with the csv import it does not find the differences.
the only way i found this to work with the csv files if I do
Compare-Object -ReferenceObject (get-ciminstance -classname Win32_Processor) -DifferenceObject (import-csv -Path $pcpusfile) -PassThru |Where-Object {$
.sideindicator -eq ‘=>’}
I’m not sure what I am missing. found lots of articles where people do the same kind of import-csv and then compare-objects and seems to work. any help is greatly appreciated

Welcome back to the forum. :wave:t3: Long time no see.

First of all … when you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in 1 <---- Click :point_up_2:t4: :wink:

(Sometimes the “preformatted text” button hides behind the settings gear symbol. :wink: )

You import the CSV files into the two variables $ccpus and $pcpus and then you use the variable $cpus for the comparison?! … have you been aware of that? Was that on purpose? I’d recommend longer and more verbose variable names to avoid those kind of confution in general. :wink:

Sorry haven’t been here in a while and forgot about the code syntax, thank you for the reminder
this is a script i use as a nagios plugin
on the first check it gathers the cpu info , ($cpus) then at the end of the scripts it saves $cpus to the file that becomes the baseline for the next check. ($pcpus)
the $cpus is the import of the current cpu check and $pcpus is the import from the previous check.
so to test it i can simply modify the file that is imported to $pcpus to force compare-object to detect the change. so $cpus and $pcus contain different data. if done via txt file import it works, if done via csv file import to the variable the compare-object is empty
you can test it just by running the

$cpus = get-ciminstance -classname Win32_Processor |select-object -Property DeviceID, Name

export this to a csv file, make a copy of that file and slightly modify the data
import-that modified file to another variable, now compare-object those to variables.
thank you

That’s my code …

Get-CimInstance -ClassName Win32_Processor | 
Select-Object -Property DeviceID, Name |
Export-Csv -Path .\CPU_01.csv -NoTypeInformation

Get-CimInstance -ClassName Win32_Processor | 
Select-Object -Property DeviceID, Name |
Export-Csv -Path .\CPU_02.csv -NoTypeInformation

Now I changed the Name property in the second CSV file

$CPU_01 = Import-Csv -Path .\CPU_01.csv
$CPU_02 = Import-Csv -Path .\CPU_02.csv

$CompareObjectSpalt = @{
    ReferenceObject  = $CPU_01
    DifferenceObject = $CPU_02
    Property         = 'Name'
    PassThru         = $true

Compare-Object @CompareObjectSpalt

And that’s the ouptut:

DeviceID Name                                SideIndicator
-------- ----                                -------------
CPU0     11th Gen Intel(R) Core(TM) i5-1240P =>
CPU0     12th Gen Intel(R) Core(TM) i5-1240P <=


I’m not using a property in the compare-object , could that be the issue?

What could happen when you tried? :neutral_face:

testing that right now

that was the issue. as soon as i added the -property ‘name’ it worked. thank you for your help. i didn’t get that from the documentation and what is still confusing is the difference between importing as text vs csv. it must be different objects in the array?