One of the problems you’re going to have with a comparison, the way you’re currently doing it, is that there will always be a difference because your CSV file contains the system name which will never be the same as the reference system. You need to make the comparison on just the service name.
Edit: just noticed you are specifying the name property . Anyway, here’s a solution not using Compare-Object
.
Example with Sample Data
# Example server CSV - replace with Import-Csv
$server = @"
Domain,SystemName,Name,DisplayName,StartMode,StartName,State
example.com,System01,DHCP,DHCP Service,Disabled,DHCP,Stopped
example.com,System01,DNS,DNS Service,Disabled,DNS,Stopped
example.com,System01,Spooler,Print Spooler,Automatic,Spooler,Started
example.com,System01,CustomApp,My Custom App,Automatic,Custom,Started
"@
$serverData = $server | ConvertFrom-CSV
# Example reference CSV - replace with Import-Csv
$reference = @"
Domain,SystemName,Name,DisplayName,StartMode,StartName,State
example.com,RefSystem,DHCP,DHCP Service,Disabled,DHCP,Stopped
example.com,RefSystem,DNS,DNS Service,Disabled,DNS,Stopped
example.com,RefSystem,Spooler,Print Spooler,Automatic,Spooler,Started
"@
$referenceData = $reference | ConvertFrom-CSV
foreach ($row in $serverData) {
if ($row.Name -notin $referenceData.Name) {
$row | Export-CSV -Path "E:\Temp\$($row.SystemName).csv" -Append
}
}
Output
PS E:\Temp> Import-Csv .\System01.csv | Format-Table
Domain SystemName Name DisplayName StartMode StartName State
------ ---------- ---- ----------- --------- --------- -----
example.com System01 CustomApp My Custom App Automatic Custom Started
Your actual code would look something like this:
$referenceData = Import-CSV -Path 'C:\Temp\KAIZEN\Non-default services detection\beevehOIIMS13.csv'
$fileList = Get-ChildItem -Path 'C:\Temp\KAIZEN\Server_service'
foreach ($file in $fileList) {
$csv = Import-Csv -Path $file.FullName
foreach ($row in $csv) {
if ($row.Name -notin $referenceData.Name) {
$row | Export-CSV -Path "C:\Temp\$($row.SystemName).csv" -Append
}
}
}