title: Powershell Hashtable Export to CSV
body: <p>I am trying to create a CSV export that contains all rows in the data spreadsheet that the IDs from the search spreadsheet show up in.</p>
  
  <p>I have managed to create the searching element through PowerShell now but am having trouble exporting the data into the CSV format.</p>
  
  <p>Below are some example tables, the actual data has up to 8 values (including ID column), but only the first three are guaranteed to be filled.</p>
  
  <p>Data Spreadsheet</p>
  
  <p><code>+------+---------+---------+---------+---------------------+
  |  ID  | Value 1 | Value 2 | Value 3 |       Value 4       |
  +------+---------+---------+---------+---------------------+
  | 1234 | London  | Serial1 | HP      | Laptop User         |
  | 2345 | Moscow  | Serial7 |         | HR Application      |
  | 1234 | London  | Serial9 |         | Finance Application |
  | 3456 | Madrid  | Serial4 | HP      | Laptop User         |
  +------+---------+---------+---------+---------------------+</code></p>
  
  <p>Search Spreadsheet</p>
  
  <p><code>+------+
  |  ID  |
  +------+
  | 1234 |
  | 2345 |
  +------+</code></p>
  
  <p>Desired Result</p>
  
  <p><code>+------+---------+---------+---------+---------------------+
  |  ID  | Value 1 | Value 2 | Value 3 |       Value 4       |
  +------+---------+---------+---------+---------------------+
  | 1234 | London  | Serial1 | HP      | Laptop User         |
  | 2345 | Moscow  | Serial7 |         | HR Application      |
  | 1234 | London  | Serial9 |         | Finance Application |
  +------+---------+---------+---------+---------------------+</code></p>
  
  <p>Below is the current code that I have with the attempts to export to CSV removed.</p>
  
  <pre><code>$finalValues = @{}
  
  $users = Import-Csv "SEARCH.csv"
  
  $data = Import-Csv "DATA.csv" | Group-Object -property ID -AsHashTable
  
  foreach ($user in $users) 
  {
      If ($data.Contains($user.ID))
      {
          #write-output $data[$user.ID].ID
          $finalValues.Add($data[$user.ID].ID, $data[$user.ID])
      }
  }
  </code></pre>
  
  <p>The following two commands (ran after the rest of the script has executed) have the below output.</p>
  
  <p><code>$finalValues.Values</code></p>
  
  <pre><code>ID      : 1234
  Value 1 : London
  Value 2 : Serial 1
  Value 3 : HP
  Value 4 : 
  Value 5 : 
  Value 6 : 
  Value 7 : Laptop User
  
  ID      : 2345
  Value 1 : Moscow
  Value 2 : Serial7
  Value 3 : 
  Value 4 : 
  Value 5 : 
  Value 6 : 
  Value 7 : HR Application
  
  ID      : 1234
  Value 1 : London
  Value 2 : Serial9
  Value 3 : 
  Value 4 : 
  Value 5 : 
  Value 6 : 
  Value 7 : Finance Application
  </code></pre>
  
  <p><code>$finalValues</code></p>
  
  <pre><code>{1234, 1234}             {@{ID=1234; Value 1=London; Value 2=Serial1; Value 3=HP; Value 4=; Value 5=; Value 6=; Value 7=Laptop User}, @{ID=1234; Value 1=London; Value 2=Serial 9... ; Value 7 =Finance Application}}
  
  2345                        {@{ID=2345; Value 1=Moscow; Value 2=Serial7; Value 3=; Value 4=; Value 5=; Value 6=; Value 7=HR Application}}           
  </code></pre>
  
  <p>When exporting to CSV with the following command I get the following result:
  <code>$finalValues | export-csv -Path test.csv -NoTypeInformation</code></p>
  
  <pre><code>+------------+-------------+----------------+--------------------------------------------+----------------------------------------------+---------------+-------+
  | IsReadOnly | IsFixedSize | IsSynchronized |                    Keys                    |                    Values                    |   SyncRoot    | Count |
  +------------+-------------+----------------+--------------------------------------------+----------------------------------------------+---------------+-------+
  | FALSE      | FALSE       | FALSE          | System.Collections.Hashtable+KeyCollection | System.Collections.Hashtable+ValueCollection | System.Object |    14 |
  +------------+-------------+----------------+--------------------------------------------+----------------------------------------------+---------------+-------+
  </code></pre>
  
  <p>When exporting to CSV with the following command I get the following result:</p>
  
  <p><code>$finalValues.Values | export-csv -Path test.csv -NoTypeInformation</code></p>
  
  <pre><code>+-------+------------+-------------+---------------+----------------+
  | Count | IsReadOnly | IsFixedSize |   SyncRoot    | IsSynchronized |
  +-------+------------+-------------+---------------+----------------+
  |     1 | FALSE      | FALSE       | System.Object | FALSE          |
  |     1 | FALSE      | FALSE       | System.Object | FALSE          |
  |     3 | FALSE      | FALSE       | System.Object | FALSE          |
  |     1 | FALSE      | FALSE       | System.Object | FALSE          |
  |     1 | FALSE      | FALSE       | System.Object | FALSE          |
  |     1 | FALSE      | FALSE       | System.Object | FALSE          |
  |     1 | FALSE      | FALSE       | System.Object | FALSE          |
  |     1 | FALSE      | FALSE       | System.Object | FALSE          |
  |     2 | FALSE      | FALSE       | System.Object | FALSE          |
  |     2 | FALSE      | FALSE       | System.Object | FALSE          |
  |     1 | FALSE      | FALSE       | System.Object | FALSE          |
  |     2 | FALSE      | FALSE       | System.Object | FALSE          |
  |     1 | FALSE      | FALSE       | System.Object | FALSE          |
  |     1 | FALSE      | FALSE       | System.Object | FALSE          |
  +-------+------------+-------------+---------------+----------------+
  </code></pre>