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 
0

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.