powershellpathexport-to-csvselect-object

Create objects with custom properties containing derived filesystem path information and export them to a CSV - calculated properties


Editor's note:
The gist of this question is:
* How do I add custom properties to objects output by Get-ChildItem that contain derived path information, namely the parent folder path (as Folder and name (as Foldername)?
* How do I export the resulting objects to a CSV file?

Currently have a script running that I took from StackOverflow and modified for my own use. The purpose of the script is to look at a directory, grab file names, then export them to a directory as a .csv file.

I was able to modify the script to pull through just the file name as originally it only pulled through the path, user etc. Was able to do this by adding ,Name. For some reason I can't get the script to pull through the parent folder it's in, also wanting the parents parent folder.

I believe this part of the code is what I am having most trouble with. Upon the select I was able to add ,Name but adding ,folder or ,foldername after the select doesn't pull through properly.

ForEach-Object {$_ | Add-Member -Name "Owner" -MemberType NoteProperty -Value (Get-Acl $_.FullName).Owner -PassThru} | 
 Sort-Object fullname | 
  Select FullName,CreationTime,LastWriteTime,Owner,Name,Folder,Foldername

Solution

  • The [System.IO.FileInfo] instances returned by Get-ChildItem for files do not have Folder or FolderName properties.

    Get-ChildItem -File $HOME\Desktop | Get-Member, for instance, will show you the available properties, and will show you that the desired information can be derived from the PSPath and PSParentPath properties.

    Select-Object allows hashtable-based property definitions, so-called calculated properties, which allow you to rename and/or transform properties and/or add custom properties derived from other property values by way of a script block.

    Note: You can also use calculated properties with the Format-Table and Format-List cmdlets for creating output for display only (as well as with other cmdlets - see the linked help topic).

    A simplified example of what you're looking for (includes output to a CSV file):

    Get-ChildItem $HOME\Desktop | Select-Object Name,
      @{ Name = 'Folder'; Expression = { Convert-Path $_.PSParentPath } },
      @{ Name = 'Foldername'; Expression = { ($_.PSPath -split '\\')[-2] } } |
        Export-Csv Out.csv -Encoding Utf8 -NoTypeInformation
    

    Note that, alternatively, you could add Folder and FolderName properties to the input objects via Add-Member, as you did with the Owner property in your question.

    Explanation:

    Note that you can get more detailed information about any of the commands mentioned by running Get-Help <command-name> -Full; add -online to view the help topic in a browser; to learn more about the -split operator, run Get-Help about_split; to learn about PowerShell's help system in general, run Get-Help Get-Help -online.