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?