We help IT Professionals succeed at work.

Powershell script to get file properties to a CSV file

I have requirement to capture following file properties using powershell.

I am using Get-childitem to get following.

$1= Get-ChildItem -recurse | Select-Object Name
$2= Get-ChildItem -recurse | Select-Object CreationTime
$3= Get-ChildItem -recurse | Select-Object LastAccessTime
$4= Get-ChildItem -recurse | Select-Object LastWriteTime
$5= Get-ChildItem -recurse | Select-Object Fullname

Then using $5 variable I wanted to run Get-Acl (only owner) to get the owner. But that is not working.
At the end I would like to have all the results in a single CSV file. like below.
Name -  Owner- CreationTime - LastAccessTime - LastWriteTime - Fullname.

I have couple of problems
1- There are file names with space in it. So when using Fullname variable ($5) I have to put double quotes..Note sure how to add double quotes.

2- Even though I get results using Get-Acl not sure how to merge all the columns in to single CSV file.

Appreciate your help.

Watch Question

PowerShell Developer
Top Expert 2010

Hey :)

Why $1 .. $5?

1. Export-Csv will deal with that for you.
2. Select them all as one object, not as 5 separate objects.

Adding a custom property here for the Owner to complete the set (@{Name='Thing';Expression={ code to get the value }}):

Get-ChildItem -Recurse | Select-Object Name, CreationTime, LastAccessTime, LastWriteTime, FullName, `
  @{n='Owner';e={ (Get-Acl $_.FullName).Owner }} | Export-Csv FileName.csv

Yell if this raises any questions :)




You are the man....I tried expression but i think i screwed it up with some comma,double & single quotes etc...In your quote i removed one single quote between FullName, `  @.

Have a good day...