Forum Discussion

Curious_Kevin16's avatar
Curious_Kevin16
Iron Contributor
Sep 02, 2022
Solved

I'm stuck on CSV generation based on a list of folders

Good Day Fellow Techies !    First time poster here but I've been benefitting from you guys for few years already 🙂   I'm trying to generate a CSV file based on a folder structure as shown below...
  • LainRobertson's avatar
    Sep 02, 2022

    Curious_Kevin16 

     

    Hey, Kev.

     

    Here's a simpler version for producing the first screenshot. Since it's not creating potentially large variables, it'll scale out significantly better, too.

     

     

    Get-ChildItem -Directory -Path "D:\Data" |
        ForEach-Object {
            $Directory = $_;
    
            Get-ChildItem -File -Path ($Directory.FullName) -Filter "*.pst" -Recurse |
                ForEach-Object {
                    [PSCustomObject] @{
                        Contoso = $Directory.Name.Replace(" ", ".");
                        FullName = $_.FullName;
                        Name = $_.Name;
                        SizeKB = [math]::Round(($_.Length / 1024), 2);
                        SizeMB = [math]::Round(($_.Length / 1048576), 2);
                    }
                }
        } | Export-Csv -NoTypeInformation -Path "D:\Data\someExportFilename.csv";

     

     

    Cheers,

    Lain

     

    Edited: To place a .Replace() on line 8, as I didn't initially spot the slight difference between username and directory name (i.e. the period between given and surnames.)

     

    Edited #2: Added in the Export-Csv command.

Resources