Luis Valencia
Luis Valencia

Reputation: 34008

Export to csv after loop ends with powershell

I have the following powershell scripts that iterates through all list items and workflows, trying to find old running workflows

The logic is OK, but I need to be able to export to to csv with the columns ListeItemUrl, ListItemName, Nr of Days Opened.

$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

Then with the exported file I can easily sort by nr of days and deliver the report I need.

Upvotes: 0

Views: 1234

Answers (1)

Raf
Raf

Reputation: 10107

Format your output as a csv - ListeItemUrl, ListItemName, NrofDaysOpened - and pipe it to Export-Csv cmdlet( you can find out more by running get-help Export-Csv). You will have to change Write-Host to Write-Output

Upvotes: 1

Related Questions