...hopefully some useful VMware related stuff

Script to list local and removable disk usage

09/10/2010 23:00

This script uses the wmi object of win32_logicaldisk and outputs to a table that displays free space on both local and removable disks and percent free. The output is shown next and script below:

 

 

SCRIPT:

 

$diskarray = gwmi win32_logicaldisk | where {$_.Size -gt 0}
$diskobject = @()
foreach($disk in $diskarray)
{
    If($disk.DriveType -eq 2)
    {
        $Type = "Removable"
    }
    ElseIf($disk.DriveType -eq 3)
    {
        $Type = "Local"
    }
    Else
    {
        $Type = "Unknown"
    }
    $totalsize = [math]::round(($disk.Size / 1024 / 1024 / 1024),1)
    $freespace = [math]::round(($disk.FreeSpace / 1024 / 1024 / 1024),1)
    $percfree = [math]::round((($freespace / $totalsize) * 100),0)
    $diskobj = "" | select Drive, Type, Name, Size, FreeSpace, PercFree
    $diskobj.Drive = $disk.DeviceID
    $diskobj.Name = $disk.VolumeName
    $diskobj.Type = $Type
    $diskobj.Size = $totalsize
    $diskobj.FreeSpace = $freespace
    $diskobj.PercFree = "  $percfree %"
    $diskobject += $diskobj
}
$diskobject | ft -AutoSize

 

Search site