none
Calculating Folder Size on a Network Share using PowerShell

    Question

  • I am trying to calculate the Size of a folder (and sub folders) as:

    $StorageLocation = \\Server1\Folder1$FolderinQuestion = "Folder2"$Measure = (Get-ChildItem ($StorageLocation + "\" + $FolderinQuestion) -Recurse | Measure-Object -Property Length -Sum)
    $Size = $Measure.Sum /1MB

    This script works perfectly fine if i use a local machine path (C:\temp  etc) in the $StorageLocation

    But as soon as I put in a path to a network share (\\server\rootshare\) for $StorageLocation, it doesn't work.  I.e.It does calculate $Measure and $Size but of the root ($StorageLocation = \\Server1\Folder1) but not of the $FolderinQuestion on $StorageLocation

    Also, when I am using network share and I try Set-location to ($StorageLocation + "\" + $FolderinQuestion), it will only set-location to ($StorageLocation) and not to ($StorageLocation + "\" + $FolderinQuestion)

    Again, no issues encountered when I am using a local paths  for $StorageLocation

    Any ideas?

    Thanks in advance.


    • Edited by shehzadkhan Monday, March 20, 2017 12:57 PM grammar
    Monday, March 20, 2017 12:35 PM

Answers

  • $StorageLocation = '\\Server1\Folder1'
    $FolderInQuestion = 'Folder2'
    $targetPath = Join-Path $StorageLocation $FolderinQuestion
    $Measure = (Get-ChildItem $targetPath -Recurse | Measure-Object Length -Sum).Sum
    
    


    \_(ツ)_/

    • Marked as answer by shehzadkhan Monday, March 20, 2017 10:28 PM
    Monday, March 20, 2017 7:58 PM

All replies

  • Also, there is no access issue. everyone has  read,write permissions to the $storgelocation

    Monday, March 20, 2017 12:42 PM
  • $StorageLocation = '\\Server1\Folder1'
    $FolderInQuestion = 'Folder2'
    $targetPath = Join-Path $StorageLocation $FolderinQuestion
    $Measure = (Get-ChildItem $targetPath -Recurse | Measure-Object Length -Sum).Sum
    
    


    \_(ツ)_/

    • Marked as answer by shehzadkhan Monday, March 20, 2017 10:28 PM
    Monday, March 20, 2017 7:58 PM
  • Perfect. works like a charm. Thanks
    Monday, March 20, 2017 10:28 PM