将数组对象导出到CSV文件

时间:2019-10-03 21:06:20

标签: arrays powershell export-csv

Am正在尝试将数组对象列表输出到CSV文件。但是,我想将目录,名称,lastwritetime和所有者放在单独的列中,而不是将它们加入一列中。不确定如何解决这种情况。请参阅下面的当前代码。到gridview和text的输出工作正常,但是csv输出却不能正常工作。

thx。

$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 

1 个答案:

答案 0 :(得分:0)

尝试一下:

$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