Search code examples
powershellget-childitemexport-csv

Powershell Export-CSV but Change Header Names


I must be missing something super simple, but due to my inexperience in Powershell, all the resources available are very in depth answers.

I have the following code which is to make a csv file with a directory's filename, modify date and creation date. It is to exclude certain file paths which are children of this directory.

Here is what I am trying to do: Upon the output of the csv, change LastWriteTime header and CreationTime header to a different tag i.e. Modified Time or something else.

What must I do to transform this code to modify the headers? I have seen answers creating arrays and then exporting values, but I feel this is an overly complicated method. What is the easiest way to accomplish changing the csv headers?

Get-ChildItem -path $pathlocation -recurse  |
 where {$_.fullname -notlike $PathsToExclude -and $_.PSIsContainer -eq $false} |
  Select-Object Name, LastWriteTime, CreationTime |
  Export-CSV -path $anotherpath -notypeinformation

The current csv headers show > "Name","LastWriteTime","CreationTime"

I want the headers to show "File", "Modify Date", "Upload Date"

Thank you in advance!


Solution

  • A solution is using Select-object as expression, for example:

    Get-ChildItem -path $pathlocation -recurse |
     where {$_.fullname -notlike $PathsToExclude -and $_.PSIsContainer -eq $false} |
      Select-Object -Property @{Name = 'File'; Expression = {$_.Name}},@{Name = 'Modify Date'; Expression = {$_.LastWriteTime}},@{Name = 'Upload Date'; Expression = {$_.CreationTime}} |
      Export-CSV -path $anotherpath -notypeinformation
    

    For more information Select-Object