Getting Physical Disk and Logical Disk Sizes

There are several use cases where it may be necessary to get the Physical Disk and Logical Disk (partition) sizes and free space. This information although easily accessible in WMI is stored in a format that is difficult to use.

These two functions should help you with that

Function Get-FreeSpace{
        [string]$computer = 'localhost',
        [string]$disk = 'C:'
    $LogicalDisks = gwmi -computername $computer -query "select * from win32_logicaldisk where deviceid='${disk}'"
    ForEach($LogicalDisk in $LogicalDisks){ 
        [pscustomobject]@{'Caption'=$LogicalDisk.caption;'Size (GB)'=$('{0:N2}' -f [double]($LogicalDisk.size/1gb));'Free (GB)'=$('{0:N2}' -f [double]($LogicalDisk.FreeSpace/1gb))}

Get-FreeSpace -computer localhost -disk 'c:'
# Output
# Caption Free (GB) Size (GB)
# ------- --------- ---------
# C:      19.38     119.24   

Function Get-PhysicalDiskSize{
        [string]$computer = 'localhost'
    $Disks = gwmi -ComputerName $computer -query "Select Index,Size,MediaType From win32_DiskDrive"
    ForEach($Disk in $Disks){
        [pscustomobject]@{"Index"=$Disk.index;"Size (GB)"=([Double]([MATH]::Round(([double]$Disk.Size / 1073741824),2)));"MediaType"=$disk.mediatype}

Get-PhysicalDiskSize -computer 'localhost'
# Output
#Index Size (GB) MediaType            
#----- --------- ---------            
#    1    465.76 Fixed hard disk media
#    0    119.24 Fixed hard disk media


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s