'Export folder size to CSV

I'm putting together a script that will import a CSV with a list of folder names (and other fields) to get folder sizes (in this case SamAccountName matches the folder names) and then export to a CSV. I can get the folder sizes but I can't get it to export to the CSV. I'm still learning PS so I'm sure this is ugly to some of you so bear with me.

Source file looks like this:

SamAccountName DisplayName Enabled HomeDirectory Office
test3 3, Test FALSE \blahblahblah\folderuserhome\subfolder\Name WASEA
test1 does not exist

Script:

function Count-Size($path)
{
  "{0:N2} KB" -f ((Get-ChildItem -Path $path -Recurse -Force | Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue).Sum / 1KB) 
}

Set-Location '\\server04\e$\tempdir'
$FolderName = Import-Csv 'E:\PS_Files\H drive names.csv' 
$FolderOut = 'E:\PS_Files\H drive sizes.csv'

$FolderSize = foreach ($name in $FolderName){
  $DirName = $Name.SamAccountName
  Count-Size $DirName
  }   

$FolderName | Select-Object *,@{Name='FolderSize';Expression={'$FolderSize'}} | Export-Csv $FolderOut -Append -NoTypeInformation

This creates the out file and passes through the existing fields but the new "FolderSize" column just has "$FolderSize" entered in the column instead of the actual folder size.

SamAccountName DisplayName Enabled HomeDirectory Office FolderSize
test3 3, Test FALSE \blahblahblah\folderuserhome\subfolder\Name WASEA $FolderSize
test1 does not exist $FolderSize

However, in PS I verified $FolderSize contains the data I'm looking for. I thought I could use a variable as an expression but maybe I'm wrong. Any ideas?



Solution 1:[1]

You certainly can use a Calculated Property to achieve what you're after. The issue lies with your loop. Regardless if you do get it to work, and changing the quotes to Double Quotes (for interoplation to occur), you will get the same size for the each folder; this is due to you not telling PowerShell to do it for each folder and export it per.

  • You basically tell PowerShell to get the Size for each and every folder path provided via SamAccountName, save all those results to $FolderSize.
  • Then, you tell it to export using '$FolderSize' which won't be interpreted due to the single quotes reading/writing as is, and the size would be the same for all folders.

Doing this in one swoop, you end up with:

$FolderOut = 'E:\PS_Files\H drive sizes.csv'
Import-Csv -Path 'E:\PS_Files\H drive names.csv' | 
    Select-Object *, @{
        Name = "FolderSize"
        Expression = {
            "{0:N2} KB" -f ((Get-ChildItem -Path $_.SamAccountName -File -Recurse -Force | Measure-Object -Property Length -Sum).Sum / 1KB)             
        }
    } #| Export-Csv -Path $FolderOut -Append -Force -NoTypeInformation

Here, you're telling PowerShell to query each folder as it comes down the pipeline, do your expression, and pipe it to Export-Csv.

Solution 2:[2]

You need to use double quotes instead of single quotes. Change '$FolderSize' to "$FolderSize".

The difference is that double quotes are evaluating the variable before printing, single quotes refer to just text.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Erick Guillen