Converting JSON to CSV using powershell
There's a good article about performance to get your started: Slow Code: Top 5 ways to make your Powershell scripts run faster; at first sight, here are applicable both parts of Problem #2 &amp; #3: Appending stuff:


Appending to files: Export-CSV -Append repeats roughly 133 times (= 4E+6/30E+3 i.e. 4E+6 rows written in chunks of 30E+3 rows each) which could considerably worsen performance for big output file;
Appending to arrays: (heart of the matter) when the += operator is used, it's actually destroying the array and creating a new one (source).


To eliminate appending to arrays, you can use .NET array list object as follows (merely a hint): replace


$output = @() with $output = [System.Collections.ArrayList]::new(), and
$output += $CSVLine with [void]$output.Add($CSVLine).


Note: do not compute one-purpose variable $CSVLine at all; instead, use

[void]$output.Add(
    [pscustomobject]@{
        Key = $json.Register.Key
        CompanyID = $json.id
        Eventtype = $item.type
        Eventdate = $item.date
        Eventdescription = $item.description
    })


Read the remark about the generic List&lt;T&gt; class and Performance Considerations as well:


  In deciding whether to use the List&lt;T&gt; or ArrayList class, both of
  which have similar functionality, remember that the List&lt;T&gt; class
  performs better in most cases and is type safe. If a reference type
  is used for type T of the List&lt;T&gt; class, the behavior of the two
  classes is identical.