title: How to include all properties of all array objects in csv in powershell
body: <p>I have a Powershell script that reads a text file with single line descriptions of shares and their properties.</p>
  
  <p>It then, for each share, adds an object to an array, with properties.</p>
  
  <p>I end up with an array of shares, many with common properties but also some that only appear on a few shares.</p>
  
  <p>When I pipe the finished array to <code>Export-CSV</code>, the resulting CSV only has columns matching the properties of the first object in the array.</p>
  
  <p>Any ideas/help gratefully received.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>$shares_only = gc \\server\share\SHARES.txt | where {$_ -match '^*TYPE = DISK'}
  
  $share_index = @()
  
  $shares_only |foreach {
  
      $sharename = ($_.Split(" ")[1])
      $_ -match '\([\w\s.,"=&lt;&gt;()\/\*\+\-]*\);'|out-null
  
      $shareparams = $matches[0]
  
      $paramlist = ($shareparams.Substring(1,($shareparams.Length -3))).Split(",")
  
      $obj = $null
      $obj = New-Object System.Object
  
      $obj | Add-Member -type NoteProperty -Name NAME -Value $sharename
  
      $paramlist | foreach {
          $obj `
          | Add-Member -type NoteProperty -Name ($_.Split("=")[0]) -Value ($_.Split("=")[1])
      }
      $share_index += $obj
  }
  
  $share_index | Select * | Export-CSV -notype \\server\share\Shares.csv
  </code></pre>
  
  <p>Here is the first two objects in the array as an example (using <code>$share_index[0..2]</code> to output to console):</p>
  
  <pre><code>NAME          : _ACCTEST_
  TYPE          :  DISK 
  PREFIX        :  (&lt;USERCODE&gt;) 
  AREABYTES     :  184320 
  ACCESS        :  ALL 
  FAMILY        :  ACCTESTPACK 
  DOWNSIZEAREA  :  TRUE 
  
  NAME              : _HOME_
  TYPE              :  DISK 
  PREFIX            :  (&lt;USERCODE&gt;) 
  COMMENT           :  Private user files 
  AREABYTES         :  184320 
  ACCESS            :  ALL -EXTRACT 
  FAMILY            :  &lt;FAMILY&gt; 
  DOWNSIZEAREA      :  TRUE 
  ALLOWGUESTACCESS  :  TRUE 
  </code></pre>
  
  <p>And here are the first 3 rows of the CSV:</p>
  
  <pre><code>"NAME","TYPE ","PREFIX ","AREABYTES ","ACCESS ","FAMILY ","DOWNSIZEAREA "
  "_ACCTEST_"," DISK "," (&lt;USERCODE&gt;) "," 184320 "," ALL "," ACCTESTPACK "," TRUE "
  "_HOME_"," DISK "," (&lt;USERCODE&gt;) "," 184320 "," ALL -EXTRACT "," &lt;FAMILY&gt; "," TRUE "
  </code></pre>
  
  <p>You can see that <code>COMMENT</code> and <code>ALLOWGUESTACCESS</code> properties are missing even although they are in the array for the <code>_HOME_</code> share.</p>
  
  <p><strong>EDIT:</strong> Accepted slightly modified version of @JPBlancs answer, so last 2 lines of my code is now:</p>
  
  <pre><code>$fixed_index = $share_index | Sort-Object -Property @{expression={(($_.psobject.Properties)|Measure-Object).count}} -Descending | ConvertTo-CSV -notype
  $fixed_index | ConvertFrom-CSV | Sort -Property Name | Export-CSV -notype \\server\share\Shares.csv 
  </code></pre>
  
  <p>So, done as suggested, then converted to CSV in a new object.
  Then, convert that object back from CSV, retaining the new properties, sort again on Name to get the alphabetical list I need, then Export to CSV again.</p>
  
  <p>Gives me, for example:</p>
  
  <pre><code>"NAME","TYPE","PREFIX","PUBLIC","COMMENT","AREABYTES","ACCESS","FAMILY","DOWNSIZEAREA","ALLOWGUESTACCESS"
  "_ACCTEST_"," DISK "," (&lt;USERCODE&gt;) ","",""," 184320 "," ALL "," ACCTESTPACK "," TRUE ",
  "_HOME_"," DISK "," (&lt;USERCODE&gt;) ",""," Private user files "," 184320 "," ALL -EXTRACT "," &lt;FAMILY&gt; "," TRUE "," TRUE "
  </code></pre>