title: Export to csv after loop ends with powershell
body: <p>I have the following powershell scripts that iterates through all list items and workflows, trying to find old running workflows</p>
  
  <p>The logic is OK, but I need to be able to export to to csv with the columns
  ListeItemUrl, ListItemName, Nr of Days Opened.</p>
  
  <pre><code>$web = get-spweb https://mysite.com/sites/billing
  $list = $web.Lists["Bill Cycles"]
  $count = 0
  foreach($wf in $list.WorkflowAssociations)
  {
     if ($wf.Name -like "*Previous Version*")
     {  
        Write-Host 'Bill Cycles with old workflow: ' $wf.Name  
        foreach($listitem in $list.Items)
        {
  
          if($listitem.ContentType.Name -eq "Bill Cycle")
          {
              $workflows = $listitem.Workflows
              foreach($Workflow in $listitem.Workflows)
              {
                  if($Workflow.AssociationId -eq $wf.Id)
                  {
                      $count = $count+1
                      Write-Host $listitem.Name
                      Write-Host 'https://mysite.com/sites/billing/'$listitem.Url.TrimStart();
                      Write-Host 'Workflow opened for: ' ((Get-Date) - $Workflow.Created).Days
                  }
              }
           }
        }
     }
  }
  Write-host 'Count: ' $count
  </code></pre>
  
  <p>Then with the exported file I can easily sort by nr of days and deliver the report I need.</p>