将数组对象导出到 CSV 文件

Export array objects to a CSV file

我正在尝试将数组对象列表输出到 CSV 文件。但是,我希望将目录、名称、上次写入时间和所有者放在单独的列中,而不是将它们连接在一列中。不确定,如何解决这种情况。请参阅下面的当前代码。 gridview 和文本的输出工作正常,但 csv 输出不是。

谢谢。

$Path = "A:\Test"
$PathArray = @()
$Results = "A:\Test\test_results_7.csv"
$extension = "xml"


# This code snippet gets all the files in $Path that end in extension parameter.
# where the file update was made in the last 30 days
Get-ChildItem $Path -Filter "*.$extension" -recurse |


Where-object { $_.LastWriteTime -ge (Get-Date).AddDays(-30)} |

ForEach-Object {
$PathArray += -join ($_.Directory , " , " , $_.Name , " , " , $_.LastWriteTime , " , " , ((Get-ACL $_.Fullname).Owner) ) 
} 

$PathArray += -join ("Path Location" , " , " , "File name" , " , " , "Last Write Time" , " , " ,  "Owner" ) 


#$PathArray   | Out-GridView 
$PathArray  | select-object $PathArray | ForEach-Object {$_} | Export-csv $Results 

试试这个:

$folders = Get-ChildItem $Path -Filter "*.$extension" -recurse | Where-object {$_.LastWriteTime -ge (Get-Date).AddDays(-30)} 
$outarray = @()

foreach($folder in $folders)
{
    $outarray += New-Object PsObject -property @{
        'Name' = $folder.FullName
        'Directory' = $folder.Directory
        'LastWrite' = $folder.LastWriteTime
    }
}

$outarray | export-csv $results

我得到了答案here