0

I have multiple reports for people in DLs I am producing where I use the below logic to combine newly added people to existing people in the DL in a CSV.

It works when there is more than 1 new user record in the newly added file, but if there's only 1 record, I get the below error and I'm not sure why?

$csv = import-csv "C:\somefile.csv"
$csv1 = import-csv "C:\somefile1.csv"

$both = $csv + $csv1 | export-csv "C:\newfile.csv"

which results in this error

 Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.
 At line:1 char:1
 + $Both = $csv + $csv1
 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException
     + FullyQualifiedErrorId : MethodNotFound

Appreciate some insights. Thanks

Tomalak
  • 332,285
  • 67
  • 532
  • 628
MarcGel
  • 299
  • 6
  • 20
  • What Powershell version are you using? (See: https://stackoverflow.com/questions/1825585/determine-installed-powershell-version) – Tomalak May 06 '19 at 16:22
  • I'm on PSVersion 5.1.14409.1018 – MarcGel May 06 '19 at 16:24
  • `Import-Csv "C:\somefile1.csv" | Export-Csv "C:\somefile.csv" -Append -NoType` – Ansgar Wiechers May 06 '19 at 16:25
  • Not sure I understand Ansgar? I need 2 csv files merged into 1. Your solution doesn't add one to the other... – MarcGel May 06 '19 at 16:34
  • Somehow the types are different? One is an object while the other is an array. How do I set the variable to an array no matter what? Looks like PS is treating the csv with the 1 record in it, like an object and I don't want that. – MarcGel May 06 '19 at 16:40
  • [`Export-Csv`](https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/export-csv?view=powershell-6) doesn't return anything. You can't assign it to a variable `$Both` (at least it is useless.) – iRon May 06 '19 at 16:40
  • It works if I hard code the variable [array]$csv. PowerShell appears to treat a csv file with only 1 record in it as an object. That's why it was failing... – MarcGel May 06 '19 at 16:43
  • the `Import-CSV` cmdlet will import multiple files if they have the same header. take a look at `Get-Help Import-Csv -Parameter Path` and you will see `-Path `. that means you can give it multiple file names ... [*grin*] – Lee_Dailey May 06 '19 at 16:44
  • 1
    The code snippet I posted appends the data rows of the second file to the first one, assuming that both files have the same columns. – Ansgar Wiechers May 06 '19 at 16:49

2 Answers2

1

Sorry to waste everyone's time. I found the answer myself... It works if I hard code the variable [array]$csv. PowerShell appears to treat a csv file with only 1 record in it as an object. The types were off. So, that's why it was failing...

MarcGel
  • 299
  • 6
  • 20
0

If you're lucky, a parameter can be an array.

import-csv file1.csv,file2.csv | export-csv file3.csv
js2010
  • 23,033
  • 6
  • 64
  • 66