Thursday, December 13, 2007

Script to extract disk space usage through WMI

This script can be used to extract information regarding sapce usage (actually free disk space in percent) from one or more servers or cluster. Since the script uses Win32_Volume, it can also recognize mountpoints.

$outData = @("")
$server = $args[0]
$dataFromServer = Get-WmiObject Win32_Volume -ComputerName $server | Select-Object SystemName,Label,Name,DriveLetter,DriveType,Capacity,Freespace

foreach ($currline in $dataFromServer) {
    if ((-not $"\\")) -and ($currline.Drivetype -ne 5)) {
        [float]$tempfloat = ($currline.Freespace / 1000000) / ($currline.Capacity / 1000000)
        $temppercent = [math]::round(($tempfloat * 100),2)
        add-member -InputObject $currline -MemberType NoteProperty -name FreePercent -value "$temppercent %"
        $outData = $outData + $currline

$outData | Select-Object SystemName,Label,Name,Capacity,FreePercent | sort-object -property FreePercent | format-table -autosize


Anonymous said...

shouldn't you be dividing by 1048576 (1024 X 1024)

Jakob Bindslet said...

dividing by 1mb (or 1024x1024) would be an option - it all depend on what "kind" of megabyte you want to have displayed. In my script above I decided to go for the 10-based MB as this is what Windows displays.

Anonymous said...

Can you provide the usage syntax to get it to work? I'm still new to PS. Thx.

Anonymous said...

I just tested it and it worked great! Thank you Jakob. :-)

Unknown said...

Thanks! that was helpful