1

Am trying to output a list of array objects to a CSV file. However, I would like to have the directory, name, lastwritetime and owner in separate columns instead of joining them in one column. Not sure, how to workaround this situation. please see the current code below. The output to gridview and text works fine, but not the csv output.

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 
Sam
  • 13
  • 3

1 Answers1

1

Try this:

$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

I got the answer here

techguy1029
  • 743
  • 10
  • 29
  • 1
    Thank you for the solution - works great. I just made a quick update to obtain the owner name in the array with the following --> 'Owner' = (Get-ACL $folder.Fullname).Owner – Sam Oct 03 '19 at 23:31