title: How can I specify the column ordering when exporting to CSV in PowerShell?
body: <p>I'm writing a script in PowerShell that exports all security groups and their members from Active Directory. Now I want to format the output of the CSV file.</p>
  <p>The code:</p>
  <pre><code>$Groups = Get-ADGroup -Properties * -Filter * -SearchBase &quot;OU=SERVICES,DC=XXXXXX,DC=XXXXX&quot; 
  
  $Table = @()
  
  $Record = @{
      &quot;Group Name&quot; = &quot;&quot;
      &quot;Name&quot; = &quot;&quot;
      &quot;Username&quot; = &quot;&quot;
  }
  
  Foreach($G In $Groups)
  {
      $Arrayofmembers = Get-ADGroupMember -identity $G -recursive | select name,samaccountname
      Foreach ($Member in $Arrayofmembers) 
      {
          $Record.&quot;Group Name&quot; = $G.Name
          $Record.&quot;Name&quot; = $Member.name
          $Record.&quot;UserName&quot; = $Member.samaccountname
          $objRecord = New-Object PSObject -property $Record
          $Table += $objrecord
      }
  }
  
  $Table | export-csv &quot;C:\temp\SecurityGroups.csv&quot; -NoTypeInformation
  </code></pre>
  <p>The result:</p>
  <pre><code>&quot;Username&quot;,&quot;Name&quot;,&quot;Group Name&quot;
  &quot;aman&quot;,&quot;Ani Manoukian&quot;,&quot;Commercial&quot;
  &quot;adan&quot;,&quot;Aurelia Danneels&quot;,&quot;Commercial&quot;
  &quot;kdeb&quot;,&quot;Kathleen De Backer&quot;,&quot;Commercial&quot;
  &quot;TVGR&quot;,&quot;Thijs Van Grimbergen&quot;,&quot;Commercial&quot;
  &quot;SVDE&quot;,&quot;Sofie Van den Eynde&quot;,&quot;Commercial&quot;
  </code></pre>
  <p><strong>Now I want the output formatted in this order:</strong></p>
  <p><code>&quot;Group Name&quot;,&quot;Name&quot;,&quot;Username&quot;</code></p>
  <p>Instead of:
  <code>&quot;Username&quot;,&quot;Name&quot;,&quot;Group Name&quot;</code></p>