I am using PowerShell to pull data and dump it into 2 different CSVs. I have this part working fine. I am using compare-item to compare the data in both CSVs. The compare is working fine however, it is not pulling all the data. I cant figure out if this has something to do with the files or with the Array.
Array Code
##Compares both CSV files and exports data
##Both files contain the headers but only File1 contains data for all the headers: "Project No.","Project Name","ProjMgr","FirstName","LastName","Status"
$file1 = import-csv -Path "C:\scriptfolder\projects.csv"
$file2 = import-csv -Path "C:\scriptfolder\archive.csv"
$Results = Compare-Object $file1 $file2 -property 'Project No.' -IncludeEqual
$Array = @()
Foreach($R in $Results)
{
If( $R.sideindicator -eq "==" )
{
$Object = [pscustomobject][ordered] @{
ProjectNumber = $R.'Project No.'
ProjectName = $R.'Project Name'
ProjectManager = $R.ProjMgr
FirstName = $R.FirstName
LastName = $R.LastName
"Compare indicator" = $R.sideindicator
}
$Array = $Object
}
}
$Array
Output:
ProjectNumber : 0049
ProjectName :
ProjectManager :
FirstName :
LastName :
Status :
Compare indicator : ==
ProjectNumber : 0205
ProjectName :
ProjectManager :
FirstName :
LastName :
Status :
Compare indicator : ==
File1 Example
"Project No.","Project Name","ProjMgr","FirstName","LastName","Status"
"0001","Project 0001 Name","005","Axl","Rose","D"
"0002","Project 0002 Name","003","Duff","McKagan","D"
"0003","Project 0003 Name","005","Steven","Adler","D"
File2 Example
"Project No.","Project Name","ProjMgr","FirstName","LastName","Status"
"0001","Project 0001 Name",,,,
"0069","Project 0069 Name",,,,
"0003","Project 0003 Name",,,,
Output File Example
"ProjectNumber","ProjectName","ProjectManager","FirstName","LastName","Status"
"0001",,,,,
"0003",,,,,
"00XX",,,,,
"00XX",,,,,
CodePudding user response:
By default, Compare-Object
outputs only the value that it compared on.
In order to make it copy all input properties to the ouput, use the -PassThru
parameter:
$Results = Compare-Object $file1 $file2 -property 'Project No.' -IncludeEqual -PassThru