Checking Hard Drive Size (Local and Remote)

by Sep 7, 2016

WMI can provide the raw data about hard drive size and free space. PowerShell then takes that information and provides user-friendly results like this:

 
PS C:\> # local
PS C:\> Get-HardDriveSize

DriveLetter Free(GB) Size(GB) Percent
----------- -------- -------- -------
C:             823,7    942,3    87,4



PS C:\> # remote
PS C:\> Get-HardDriveSize -ComputerName server2 -Credential server2\Tobias

DriveLetter Free(GB) Size(GB) Percent
----------- -------- -------- -------
C:              87,3    436,9      20
D:               5,3       25    21,3
 

Here is the code:

function Get-HardDriveSize
{
  param
  (
    $ComputerName,

    $Credential
  )

  # get calculated properties:
  $prop1 = @{
    Name = 'DriveLetter'
    Expression = { $_.DeviceID }
  }

  $prop2 = @{
    Name = 'Free(GB)'
    Expression = { [Math]::Round(($_.FreeSpace / 1GB),1) }
  }

  $prop3 = @{
    Name = 'Size(GB)'
    Expression = { [Math]::Round(($_.Size / 1GB),1) }
  }

  $prop4 = @{
    Name = 'Percent'
    Expression = { [Math]::Round(($_.Freespace * 100 / $_.Size),1) }
  }

  # get all hard drives
  Get-CimInstance -ClassName Win32_LogicalDisk @PSBoundParameters -Filter "DriveType=3" | 
  Select-Object -Property $prop1, $prop2, $prop3, $prop4

}

Twitter This Tip! ReTweet this Tip!