title: Powershell Export CSV to XML
body: <p>I need to convert a csv file to XML that contains 7 columns:</p>
  <pre><code>network;server;instance;type;date;time;state
  toto;titi;APPLINT1;LOG;10/02/2021;13:00:10 - During in min : 1;Succeeded
  toto;titi;APPLINT1;VIDEO;12/02/2021;13:20:10 - During in min : 1;Succeeded
  toto;tutu;SPTPROD1;DIFF;10/02/2021;14:30:10 - During in min : 1;Succeeded
  toto;tutu;TOOL;DIFF;12/02/2021;14:00:10 - During in min : 1;Succeeded
  </code></pre>
  <p>The XML structure should be as the following:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;xml&gt;
          &lt;network name='toto'&gt;
                  &lt;server name='titi'&gt;
                          &lt;instance name='APPLINT1'&gt;
                              &lt;type name='LOG'&gt;
                                  &lt;date name=&quot;10/02/2021&quot;&gt;
                                      &lt;time name=&quot;13:00:10 - During in min : 1&quot;&gt;
                                          &lt;state name=&quot;Succeeded&quot;&gt;&lt;/state&gt;
                                      &lt;/time&gt;
                                      &lt;time name=&quot;14:00:10 - During in min : 1&quot;&gt;
                                          &lt;state name=&quot;Succeeded&quot;&gt;&lt;/state&gt;
                                      &lt;/time&gt;
                                  &lt;/date&gt;
                              &lt;/type&gt;
                              &lt;type name='DIFF'&gt;
                                  &lt;date name=&quot;10/02/2021&quot;&gt;
                                      &lt;time name=&quot;13:00:10 - During in min : 1&quot;&gt;
                                          &lt;state name=&quot;Succeeded&quot;&gt;&lt;/state&gt;
                                      &lt;/time&gt;
                                      &lt;time name=&quot;14:00:10 - During in min : 1&quot;&gt;
                                          &lt;state name=&quot;Succeeded&quot;&gt;&lt;/state&gt;
                                      &lt;/time&gt;
                                  &lt;/date&gt;
                              &lt;/type&gt;
                              &lt;type name='FULL'&gt;
                                  &lt;date name=&quot;10/02/2021&quot;&gt;
                                      &lt;time name=&quot;13:00:10 - During in min : 1&quot;&gt;
                                          &lt;state name=&quot;Succeeded&quot;&gt;&lt;/state&gt;
                                      &lt;/time&gt;
                                      &lt;time name=&quot;14:00:10 - During in min : 1&quot;&gt;
                                          &lt;state name=&quot;Succeeded&quot;&gt;&lt;/state&gt;
                                      &lt;/time&gt;
                                  &lt;/date&gt;
                              &lt;/type&gt;
                          &lt;/instance&gt;
                          &lt;instance name='VIDEO'&gt;
                              &lt;type name='LOG'&gt;
                                  &lt;date name=&quot;10/02/2021&quot;&gt;
                                      &lt;time name=&quot;13:00:10 - During in min : 1&quot;&gt;
                                          &lt;state name=&quot;Succeeded&quot;&gt;&lt;/state&gt;
                                      &lt;/time&gt;
                                      &lt;time name=&quot;14:00:10 - During in min : 1&quot;&gt;
                                          &lt;state name=&quot;Succeeded&quot;&gt;&lt;/state&gt;
                                      &lt;/time&gt;
                                  &lt;/date&gt;
                              &lt;/type&gt;
                          &lt;/instance&gt;
                  &lt;/server&gt;
                  &lt;server name='tutu'&gt;
                          &lt;instance name='SPTPROD1'&gt;
                              &lt;type name='LOG'&gt;
                                  &lt;date name=&quot;10/02/2021&quot;&gt;
                                      &lt;time name=&quot;13:00:10 - During in min : 1&quot;&gt;
                                          &lt;state name=&quot;Succeeded&quot;&gt;&lt;/state&gt;
                                      &lt;/time&gt;
                                      &lt;time name=&quot;14:00:10 - During in min : 1&quot;&gt;
                                          &lt;state name=&quot;Succeeded&quot;&gt;&lt;/state&gt;
                                      &lt;/time&gt;
                                  &lt;/date&gt;
                              &lt;/type&gt;
                              &lt;type name='DIFF'&gt;
                                  &lt;date name=&quot;10/02/2021&quot;&gt;
                                      &lt;time name=&quot;13:00:10 - During in min : 1&quot;&gt;
                                          &lt;state name=&quot;Succeeded&quot;&gt;&lt;/state&gt;
                                      &lt;/time&gt;
                                      &lt;time name=&quot;14:00:10 - During in min : 1&quot;&gt;
                                          &lt;state name=&quot;Succeeded&quot;&gt;&lt;/state&gt;
                                      &lt;/time&gt;
                                  &lt;/date&gt;
                              &lt;/type&gt;
                              &lt;type name='FULL'&gt;
                                  &lt;date name=&quot;10/02/2021&quot;&gt;
                                      &lt;time name=&quot;13:00:10 - During in min : 1&quot;&gt;
                                          &lt;state name=&quot;Succeeded&quot;&gt;&lt;/state&gt;
                                      &lt;/time&gt;
                                      &lt;time name=&quot;14:00:10 - During in min : 1&quot;&gt;
                                          &lt;state name=&quot;Succeeded&quot;&gt;&lt;/state&gt;
                                      &lt;/time&gt;
                                  &lt;/date&gt;
                              &lt;/type&gt;
                          &lt;/instance&gt;
                          &lt;instance name='VIDEO'&gt;
                              &lt;type name='LOG'&gt;
                                  &lt;date name=&quot;10/02/2021&quot;&gt;
                                      &lt;time name=&quot;13:00:10 - During in min : 1&quot;&gt;
                                          &lt;state name=&quot;Succeeded&quot;&gt;&lt;/state&gt;
                                      &lt;/time&gt;
                                      &lt;time name=&quot;14:00:10 - During in min : 1&quot;&gt;
                                          &lt;state name=&quot;Succeeded&quot;&gt;&lt;/state&gt;
                                      &lt;/time&gt;
                                  &lt;/date&gt;
                              &lt;/type&gt;
                          &lt;/instance&gt;
                  &lt;/server&gt;
  &lt;/xml&gt;
  
  </code></pre>
  <p>Powershell script :</p>
  <pre><code>$docTemplate = @'
  &lt;xml&gt;
      &lt;network name=$($backup.network)&gt;
          &lt;server name=$($backup.server)&gt;
              &lt;instance name=$($backup.instance)&gt;
                  &lt;type name=$($backup.type)&gt;
  $($backups -join &quot;`n&quot;)
                  &lt;/type&gt;
              &lt;/instance&gt;
          &lt;/server&gt;
      &lt;/network&gt;
  &lt;/xml&gt;
  '@
  
  # Per-backup template.
  $entryTemplate = @'
                      &lt;date name=$($backup.date)&gt;
                          &lt;time name=$($backup.time)&gt;
                              &lt;state name=$($backup.state)&gt;&lt;/state&gt;
                          &lt;/time&gt;
                      &lt;/date&gt;
  '@
  
  Import-Csv backup_sql.csv -Delimiter ';' | Group-Object instance -ov grp | ForEach-Object {
      $backups = foreach ($backup in $_.Group) {
          $ExecutionContext.InvokeCommand.ExpandString($entryTemplate)  
      }
      $ExecutionContext.InvokeCommand.ExpandString($docTemplate)
  } | Out-File &quot;backup_sql.xml&quot;
  </code></pre>
  <p>Actually XML output file :</p>
  <pre><code>&lt;xml&gt;
      &lt;network name=toto&gt;
          &lt;server name=titi&gt;
              &lt;instance name=APPLINT1&gt;
                  &lt;type name=LOG&gt;
                      &lt;date name=10/02/2021&gt;
                          &lt;time name=13:00:10 - During in min : 1&gt;
                              &lt;state name=Succeeded&gt;&lt;/state&gt;
                          &lt;/time&gt;
                      &lt;/date&gt;
                      &lt;date name=10/02/2021&gt;
                          &lt;time name=13:00:10 - During in min : 1&gt;
                              &lt;state name=Succeeded&gt;&lt;/state&gt;
                          &lt;/time&gt;
                      &lt;/date&gt;
                  &lt;/type&gt;
              &lt;/instance&gt;
          &lt;/server&gt;
      &lt;/network&gt;
  &lt;/xml&gt;
  &lt;xml&gt;
      &lt;network name=toto&gt;
          &lt;server name=titi&gt;
              &lt;instance name=SPTPROD1&gt;
                  &lt;type name=DIFF&gt;
                      &lt;date name=10/02/2021&gt;
                          &lt;time name=14:30:10 - During in min : 1&gt;
                              &lt;state name=Succeeded&gt;&lt;/state&gt;
                          &lt;/time&gt;
                      &lt;/date&gt;
                  &lt;/type&gt;
              &lt;/instance&gt;
          &lt;/server&gt;
      &lt;/network&gt;
  &lt;/xml&gt;
  &lt;xml&gt;
      &lt;network name=toto&gt;
          &lt;server name=tutu&gt;
              &lt;instance name=TOOL&gt;
                  &lt;type name=DIFF&gt;
                      &lt;date name=10/02/2021&gt;
                          &lt;time name=14:00:10 - During in min : 1&gt;
                              &lt;state name=Succeeded&gt;&lt;/state&gt;
                          &lt;/time&gt;
                      &lt;/date&gt;
                  &lt;/type&gt;
              &lt;/instance&gt;
          &lt;/server&gt;
      &lt;/network&gt;
  &lt;/xml&gt;
  &lt;xml&gt;
      &lt;network name=toto&gt;
          &lt;server name=tutu&gt;
              &lt;instance name=SKYPE&gt;
                  &lt;type name=LOG&gt;
                      &lt;date name=10/02/2021&gt;
                          &lt;time name=23:00:10 - During in min : 1&gt;
                              &lt;state name=Failed&gt;&lt;/state&gt;
                          &lt;/time&gt;
                      &lt;/date&gt;
                  &lt;/type&gt;
              &lt;/instance&gt;
          &lt;/server&gt;
      &lt;/network&gt;
  &lt;/xml&gt;
  </code></pre>
  <p>I have based myself on the subject below : <a href="https://stackoverflow.com/questions/52969408/powershell-csv-to-xml">Powershell CSV to XML</a></p>
  <p>But unfortunately I cannot adapt it for the different columns.</p>
  <p>The goal is to have the state of the different SQL backups (logs, diff and full) of the multiple SQL instances of the different servers ...</p>
  <p>thank you in advance for your help!</p>