Exporting Foreach loop output to CSV

Hello there,

I am rather new to Powershell. I wrote the below code to extract properties out of some files and export to CSV.

$output = foreach ($doc in $docs)
{
    $date = Get-Date
    $duration = ($date - $doc.StatusChangeDate).TotalHours
    $Name = $doc.FileName
    $outto = $doc.DocumentOutToName
    $fullpath =$doc.FullPath
    $statuschange = $doc.StatusChangeDate
    @($Name,$duration,$outto,$statuschange,$fullpath)
}
$output
$output | export-csv -Path "C:\Users\mypc\Desktop\Powershell\status.csv" -NoTypeInformation

I am getting the output as
1a
1b
1c
1d
1e
2a
2b
2c
2d
2e
3a …

But somehow the export to csv is not giving any output. I want the output in below format.
1a 1b 1c 1d 1e
2a 2b 2c 2d 2e

Also, please let me know anything I can do to enhance the quality of the code.

The way you collect the properties does not create any relation between them.

You did not show how you create the variable $docs.

Instead of a plain list of variables you could create a PSCustomObject and ouput this. Please read the help completely to learn how to use it:

1 Like

I got the variable $docs from ProjectWise, a file management system. This variable contains some documents with some properties associated with it. Let me try PSCustomObject for this.