Last week @beningus, also a Dutch PowerShell interested IT Pro, contacted me for some assistance troubleshooting a script.
The purpose of that script was to list the directories of, for example, the homedirs on a fileserver in order to find the “big spenders” among the users. For this, we’ve written the following function:
1 2 3 4 5 6 7 8 9 10 11 12 |
function Get-FolderLength { # Usage: Get-FolderLength.ps1 D:\Homedirs param ( [Parameter(Mandatory=$true)] [String]$Path ) $FileSystemObject = New-Object -com Scripting.FileSystemObject $folders = (Get-Childitem $path | ? {$_.Attributes -eq "Directory"}) foreach ($folder in $folders) { $folder | Add-Member -MemberType NoteProperty -Name "SizeMB" –Value(($FileSystemObject.GetFolder($folder.FullName).Size) / 1MB) } $folders | sort -Property SizeMB -Descending | select fullname,@{n=’Size MB’;e={"{0:N2}" –f $_.SizeMB}} } |
Does not work. Do I need to add extra code to make it work or this is supposed to be completed code? It generates the following error:
Get-FolderLength : Cannot bind argument to parameter ‘Path’ because it is an empty string.
At line:14 char:17
+ Get-FolderLength <<<< $args[0]
+ CategoryInfo : InvalidData: (:) [Get-FolderLength], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Get-FolderLength
Hi Antonio.,
Changed the code, now it works.
Thanks for the feedback.
Jeff.
For me, this returns 0MB for all folders found, please help!
Hi Boycie,
I’ve just tested the script in a newly installed VM, works for me…
The only situations I could think of where it would give 0MB for the directories is where they are empty, have less than 0,005 MB of data in them or when there is not access to the date in the directories.
Could you test and/or confirm this?
Jeff.
Jeff ,
love your script thanks for the help
Hello Jeff. Can this script be modified to search for certain extensions on a fileshare and then report them by mail to the sysadmin?
Hi Hein,
That wouldn’t be a modification but an entire new script 😉
Jeff.
Hi Jeff
This works great! But I’m stuck with 1 folder which does not return a size at all.
If I do $objFSO.GetFolder($Folder.Fullname).Size it is empty … not even 0MB
Maybe I forgot to mention ….
– There’s plenty of data in this folder
– I’ve got full access to the whole folder hierarchy
Hi Philip,
If it works for all other folders, but not for just one then I would recommend a game of ‘look for the difference’.
Think about folder attributes and such.
If you find a difference (there has to be one), let me know and I can investigate / solve.
Jeff.
perhaps you can run it step-by step via Powershell ISE