What is the best way to get used space and available space?


  • Hello

    I have used following code for get the used space. First line of code respond time  is very slow and second line of code is faster then earlier. Is there any other way to get better performance?

    $RawSize = (Get-ChildItem "\\\HOME005\FirstName.LastName\Home" -Recurse | Measure-Object Length -Sum).Sum
    $size1 = (robocopy "\\\HOME005\FirstName.LastName\Home" NULL /E /L /NFL /NDL /NC /FP /BYTES /NP /NJH /r:0 /w:0 | ? {$_ -match "Bytes :"}).trim("Bytes :").split(" ")[0]

    I also want to get available space in better way. I posted in another on freespace, but did not got any better response. I am mainly looking for available space or freespace on the user machine, but it should return the result very fast.

    I want to includes all above code in my existing script.

    Please advise


    Friday, April 21, 2017 8:52 AM

All replies

  • Neither method will give you that.  You must use WMI to query the drive under the share.

    Get-WmiObject Win32_Share -ComputerName

    Then get the volume and check used space and freespace.

    Get-WmiObject Win32_Volume -Filter 'DriveLetter="C:"' -ComputerName|select Capacity, FreeSpace


    Friday, April 21, 2017 9:11 AM
  • $computer = ""
    $disks = get-wmiobject Win32_LogicalDisk -computername $computer -Filter "DriveType = 3"
    foreach ($disk in $disks){
    $letter = $disk.deviceID
    $volumename = $disk.volumename
    $totalspace = [math]::round($disk.size /1GB,2)
    $freespace = [math]::round($disk.freespace /1GB,2)
    $usedspace = [math]::round(($disk.size - $disk.freespace) /1GB,2)
    $disk | select @{n="Computer Name";e={$computer}}, @{n="Disk Letter";e={$letter}}, @{n="Volume Name";e={$volumename}}, @{n="Total Space";e={($totalspace).tostring() + " GB"}}, @{n="Free Space";e={($freespace).tostring() + " GB"}}, @{n="Used Space";e={($usedspace).tostring() + " GB"}}

    Friday, April 21, 2017 9:18 AM
  • Hi JRV,

    When I run

    Get-WmiObject Win32_Share -ComputerName \\\HOME005\FirstName.LastName\Home (this is mapped folder with file server)

    It is showing "Invalid Parameter", am I missing anything


    • Edited by Avian123 Friday, April 21, 2017 10:50 AM
    Friday, April 21, 2017 10:46 AM
  • I recommend that you take some time learning basic Windows and the Windows file system.

    What you have used is NOT a computer name.  It is a share name.  As I posted, you cannot get freespace on a  share.  It is impossible and makes no sense.  You must get the shares definition from the remote computer and then use the volume that the share is on to get the usage numbers.

    First learn about the file system and how sharing works.  What is a share?  What does it so?  Why is a share not a file system, drive or volume?

    Without this basic knowledge you will not understand what is happening or how to manage the resources.

    You can also enable quotas on the underlying user folders and then query the quota system to get usage by user.  You will still need to use WMI to get the freespace on the volume.


    Friday, April 21, 2017 10:53 AM
  • Thanks for suggestion. I am still learning.

    However I don't need freespace there from shared location. I need usedspace information from shared location.

    Freespace inform need from usermachine, and you second line of code it is running correctly. I am available to get the available space.

    Friday, April 21, 2017 11:03 AM
  • Can anyone guide me on above mentioned post?
    Friday, April 21, 2017 2:35 PM
  • wmi is a service on a windows machine that allows you to poll the OS for pertinent information, os version/hd space/total ram. . . . basically if there is a counter for it in perfmon you can pull the info from WMI.

    Edit: you target a machine, not a windows share.

    • Edited by The Grim Friday, April 21, 2017 3:08 PM
    Friday, April 21, 2017 3:08 PM