首页 > 解决方案 > 将数组对象导出到 CSV 文件

问题描述

我正在尝试将数组对象列表输出到 CSV 文件。但是,我希望将目录、名称、lastwritetime 和所有者放在单独的列中,而不是将它们合并到一个列中。不确定,如何解决这种情况。请参阅下面的当前代码。输出到 gridview 和 text 工作正常,但不是 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 

标签: arrayspowershellexport-csv

解决方案


尝试这个:

$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

我在这里得到了答案


推荐阅读