Calculate Folder size of all users downloads folder on our server

Hello,

I want to make an overview of all the download folders on users homedrives. I have created this script but somehow something is going wrong in the calculation of the sizes. My custom object is filled with the right folders but the calculation all show the same number, it must be the size of one of the homedrives. What am i doing wrong?

Mapnaam is foldername in dutch, grootte is size.

 

$basefolder = "E:\Shares\Users"

$allfolders = $basefolder | Get-ChildItem -Directory -Recurse

$folderoverview = @()

Foreach ($folder in $allfolders){

If ($folder.FullName -like "*Downloads"){

$calculate = (Get-ChildItem $folder -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop)

$folderoverview += (New-Object PSObject -Property @{

Mapnaam = $folder.Fullname

Grootte = "{0:N2}" -f ($calculate.sum / 1MB) + " MB "

})

}

}

$folderoverview | select Mapnaam,Grootte | Sort-Object Grootte -Descending

Here’s how I would do it…

$BaseDir = "E:\Shares\users"

# I wouldn't use Recurse here, cause that could be a very slow 
# process, and seems unnecessary.
$UserFolders = Get-ChildItem -Path $BaseDir -Directory


# Everything next we do we store in $Items
$Items = foreach ($UF in $UserFolders) {
    # Foreach UserFolder
    # Go To the "<UserFolderPath>\Downloads", and measure everything in that folder, sum it up,
    # and save it into the Sum variable
    $Sum = (Get-ChildItem -Path "$($UF.FullName)\Downloads" -Recurse | measure-object -Property length -sum).Sum

    # Create a new object with the FolderName, Size in Human Readable Format, and Raw Size
    # (we could do other things for the Human Readable, but we'll skip that here)
    New-Object -TypeName PSObject -Property @{
        Mapnaam = $UF.Name
        Grootte = ("{0:n2} GB" -f ($Sum / 1gb))
        GrootteByte = $Sum
    }
    # Each iteration through the loop, we write 1 object to the pipeline, meaning that $Items
    # will contain an Array of objects
}

# Then we can write the Items to wherever you need to
# $Items | export-csv -path <pathToCsvFile>\csvFile.csv -notypeinf
# $Items | export-clixml -path <path>\xmlFile.xml
# et cetera

 

Thanks a lot! This did exactly what i wanted to achieve. Im fairly new to powershell (really enjoying it though!) so some basic things give me struggles some times :slight_smile: