locked
ExportCSV Column Object Not Exporting RRS feed

  • Question

  • the script below outputs my desired info except that the Folder.Parent is the same for all entries instead of the actual entry in the supplied CSV file.

    $FolderList = "C:\FolderList1.csv" $FolderSizes = "C:\FolderSizes1.csv" $Folders = Import-csv $FolderList -Delimiter "," $oOUtputItems = @() foreach ($Folder in $Folders.Fullname) { Write-Host "Calculating folder size for: " -NoNewline; Write-host $Folder -ForegroundColor Green $FolderSize = (Get-ChildItem $folder -file -Recurse -ErrorAction SilentlyContinue | Measure-Object -property length -sum -ErrorAction SilentlyContinue) $Parent = (@($Folders.Parent) | Out-String).trim() $oOUtputItems += New-Object -TypeName PSObject -Property ` @{ 'Parent'=($folders.Parent) Folder = $Folder; 'Size in MB' = (("{0:N0}" -f ($FolderSize.sum / 1MB))) } | Select-Object Parent, Folder, 'Size in MB' | Export-csv -NoTypeInformation $FolderSizes -Append } Invoke-Item $FolderSizes



    Tuesday, January 8, 2019 3:17 PM

Answers

  • This would be easier to understand, use and debug:

    $FolderList = 'C:\FolderList1.csv'
    $FolderSizes = 'C:\FolderSizes1.csv'
    
    Import-csv $FolderList |
        ForEach-Object{
            Write-Host "Calculating folder size for: $($_.FullName)"
            $FolderSize = Get-ChildItem $_.FullName -file -Recurse | Measure-Object -property length -sum 
            [pscustomobject]@{
                Parent     = $_.Parent
                Folder     = $_.Fullname
                'Size in MB' = '{0:N0}' -f $FolderSize.sum/1MB
            }
        } |
        Select-Object Parent, Folder, 'Size in MB' |
        Export-csv $FolderSizes -NoTypeInformation
    
    Invoke-Item $FolderSizes

    Input CSV must have "Parent", And "Fullname" in the header.


    \_(ツ)_/




    • Edited by jrv Tuesday, January 8, 2019 4:15 PM
    • Marked as answer by TheAnalogKid Tuesday, January 8, 2019 4:18 PM
    Tuesday, January 8, 2019 4:14 PM

All replies

  • But you use $Folders.Parent instead of $Folder.Parent.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Tuesday, January 8, 2019 3:50 PM
  • If I change it to  folder.Parent the parent column in the output file is empty.

    Tuesday, January 8, 2019 4:03 PM
  • This would be easier to understand, use and debug:

    $FolderList = 'C:\FolderList1.csv'
    $FolderSizes = 'C:\FolderSizes1.csv'
    
    Import-csv $FolderList |
        ForEach-Object{
            Write-Host "Calculating folder size for: $($_.FullName)"
            $FolderSize = Get-ChildItem $_.FullName -file -Recurse | Measure-Object -property length -sum 
            [pscustomobject]@{
                Parent     = $_.Parent
                Folder     = $_.Fullname
                'Size in MB' = '{0:N0}' -f $FolderSize.sum/1MB
            }
        } |
        Select-Object Parent, Folder, 'Size in MB' |
        Export-csv $FolderSizes -NoTypeInformation
    
    Invoke-Item $FolderSizes

    Input CSV must have "Parent", And "Fullname" in the header.


    \_(ツ)_/




    • Edited by jrv Tuesday, January 8, 2019 4:15 PM
    • Marked as answer by TheAnalogKid Tuesday, January 8, 2019 4:18 PM
    Tuesday, January 8, 2019 4:14 PM
  • Thank you very much. You assistance is greatly appreciated.
    Tuesday, January 8, 2019 4:18 PM