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 for a urgent requirement. Each folder has multiple PST files in them (some has 1 and some may have even 4or 5 in the real scenario). I basically want to generate a CSV with file path for each PST file with the respective username. 

 

Folder structure: C:\Tools\PSTTest\PST

Files: in each folder/s:

 

Expected result be like this: but unfortunately I struggled to fix it for few hours now 

 

Issues:

1. in my current PS Script, its all gets mixed (Other's entries also falls under Bill.Gates and repeats across the others as well so its all messy)

2. and also generates an extra .PST at the end of the every item. 

 

Current output CSV: 

 

My Script: 

 

$MigrationList = import-csv "C:\Tools\PSTTest\CSV\SourceUserList.csv"
$ContosoUsers = $MigrationList.SamAccountName | sort -Unique

$root_folders = dir "C:\Tools\PSTTest\PST" -Directory | select fullname
$user_pst_list = @()
$user_pst_list_folder = @()

$Date = Get-Date -Format yyyy-MM-dd

foreach($user in $ContosoUsers)
{
    $Contoso = $user

    foreach($folder in $root_folders)
    {
        $folder_path = $folder.fullname
        $user_pst = dir "$folder_path\" -Recurse -Include *.pst -ErrorAction SilentlyContinue | select @{label="Contoso";expression={$Contoso}},fullname,name,@{label="SizeKB";expression={($_.length) / 1024}},@{label="SizeMB";expression={[math]::Round((($_.length) / 1048576),2)}} | ?{$_.SizeKB -gt 265}
        if($user_pst)
        {
            $user_pst_path = $user_pst.fullname
            $user_pst_list += $user_pst
            $user_pst_folder = get-item "$folder_path\$Contoso" -ErrorAction SilentlyContinue | select @{label="Contoso";expression={$Contoso}},fullname

            if($user_pst_folder)
            {
                $user_pst_list_folder += $user_pst_folder
            }
        }
    }
}

$user_pst_list | export-csv "C:\Tools\PSTTest\CSV\pst_list_$date.csv" -nti

 

 

If you can help out showing me the issue in this script I would be very grateful ! Thank you very much !

 

-Kevin

  • 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.

6 Replies

  • LainRobertson's avatar
    LainRobertson
    Silver Contributor

    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.

    • Curious_Kevin16's avatar
      Curious_Kevin16
      Iron Contributor
      LainRobertson

      Thank you so much for quick turnaround ! really appreciate it. If you could update this very same code for export to CSV piece that'd be fantastic !

      Thank you again Lain

Resources