title: Powershell output information by heading variable from CSV
body: <p>I'm trying to take a csv file with a couple of headings and format it into a nice word document while sorting alphabetically by heading1.</p>
  <p>The csv looks like:</p>
  <pre><code>heading1,heading2,heading3, - up to heading5
  info1,info2....info5
  info1,info2....info5
  etc etc
  </code></pre>
  <p>Heading1 has a lot of double-ups so the script will look something like:
  If info1 is the same as the above info1, just print info2 and info3.
  Else print info 1-3
  The problem is I don't understand how to assign variables to the headings in my ForEach loops.</p>
  <p>Here is what I've tried:</p>
  <pre><code>$previousheading1 = &quot;&quot;
  
  $FormatCSV = Import-Csv -Path $RawCSV.Filename -Header &quot;Heading1&quot;, &quot;Heading2&quot;, &quot;Heading3&quot;, &quot;Heading4&quot;, &quot;Heading4&quot; | Sort-Object -Property Heading1 
  
  $Heading1= $FormatCSV | Select-Object -ExpandProperty Heading1
  $Heading2= $FormatCSV |  Select-Object -ExpandProperty Heading2
  $heading3= $FormatCSV |  Select-Object -ExpandProperty Heading3
  
  $FormatCSV = ForEach-Object {
      if ($heading1 -eq $previousHeading1) {
          Write-Output (&quot;$Heading2- $Heading3&quot;)
          Write-Output (&quot;`v&quot;)
      }
      else {
          Write-Output (&quot;`v&quot;)
          Write-Output (&quot;$Heading1&quot;)
          Write-Output (&quot;$Heading2- $Heading3&quot;)
      }
      $previousHeading1 = ($Heading1)
  }
  </code></pre>
  <p>At the moment I'm not getting any output in the terminal so something's clearly going wrong.</p>
  <p>I have also tried:</p>
  <pre><code>$FormatCSV = Import-Csv -Path $RawCSV.Filename -Header &quot;heading1&quot;, &quot;heading2&quot;, &quot;heading3&quot;, &quot;heading4&quot;, &quot;heading5&quot; | Sort-Object -Property tenant
  $FormatCSV #This just shows that each entry has been sorted correctly into the nice four lines
  
  Write-Output &quot;The list should be below this&quot; #This is only for the sake of my sanity
  
  $previousHeading1 = &quot;&quot;
  
  foreach ($FormatCSV in $FormatCSV.heading1) {
      if ($FormatCSV.heading1-eq $previousHeading1) {
          Write-Output ($FormatCSV.heading2, $FormatCSV.heading3)
  
      }
      else {
          Write-Output ($FormatCSV.heading1)
          Write-Output ($FormatCSV.heading2, $FormatCSV.heading3)
      }
      $previousHeading1 = ($FormatCSV.heading1)
  }
  </code></pre>
  <p>The second line ($FormatCSV) I left in so I can see how the data is being organised in the terminal. I have the feeling this is a simple fix and I've missed it. Thanks in advance for the help :)</p>
  <p>If it help, I'm using visual basic studio and running this in powershell.</p>