Calculate Folder size of all users downloads folder on our server

Welcome Forums General PowerShell Q&A Calculate Folder size of all users downloads folder on our server

Viewing 2 reply threads
  • Author
    Posts
    • #226128
      Participant
      Topics: 1
      Replies: 1
      Points: -6
      Rank: Member

      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
      • This topic was modified 2 weeks, 6 days ago by Arjen150235.
    • #226203
      Participant
      Topics: 3
      Replies: 12
      Points: 78
      Rank: Member

      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

       

      • This reply was modified 2 weeks, 6 days ago by mitchvh05.
    • #226281
      Participant
      Topics: 1
      Replies: 1
      Points: -6
      Rank: Member

      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 🙂

Viewing 2 reply threads
  • You must be logged in to reply to this topic.