Forum Discussion
I'm stuck on CSV generation based on a list of folders
- Sep 02, 2022
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.
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
- Curious_Kevin16Sep 02, 2022Iron ContributorLainRobertson
Somehow It slipped from my mind that I was referring to that CSV because it also had some fields that I needed to retain in the output 😞
i.e. - EmailAddress (email address removed for privacy reasons) , ArchiveStatus (Active/Inactive)- LainRobertsonSep 02, 2022Silver Contributor
Here's a re-worked example where the CSV drives the folders checked.
It does make an assumption that the folder name is precisely the same as the sAMAccountName, just with any periods from the sAMAccountName being replaced by spaces, since that's all I can gather from the screenshots.
Because I can only see a reference to sAMAccountName from the CSV, I've included a single example on line 11 that demonstrates how to include the CSV data in the output but you should be able to figure out how to add any others that you want to include.
Alternatively, if you can't figure that out, you'll need to provide us with an example of the CSV file, including the headers so that we have something to guide us on what is in that file. Right now, we can't even guess, really.
Import-Csv -Path "D:\Data\yourInputData.csv" | ForEach-Object { $CsvData = $_; # Store the current CSV row for future reference later in the script. $FolderName = "D:\Data\PstRootFolder\$($CsvData.sAMAccountName.Replace(".", " "))"; if (Test-Path -Path $FolderName) # Check that the folder exists. { Get-ChildItem -File -Path $FolderName -Filter "*.pst" -Recurse | ForEach-Object { [PSCustomObject] @{ Contoso = $CsvData.sAMAccountName; # This shows how you can refer back to the CSV data and bring in any columns found in that file. 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
- Curious_Kevin16Sep 02, 2022Iron Contributor
Worked perfectly!
Thanks very much again Lain, this is the real beauty of tech community. Have a fantastic weekend.