WMI Query to Calculate Disk Free SPace Percentage

Kedar Tamboli 166 Reputation points
2022-05-05T12:21:01.98+00:00

Hello Team,

I want to configure Task Sequence Option with WMI Query condition. Remediation script should run if condition met:

If C: Free Space is less than 10% of total C: OR C: Free Space is less than 5 GB then remediation script will run.

Please suggest if below WMI query is correct or needs modification.

"Select * from Win32_LogicalDisk WHERE (NAME LIKE 'C:') AND ((FREESPACE <= '5368709120') OR (FREESPACE/SIZE <= '0.1'))"

Microsoft Configuration Manager Deployment
Microsoft Configuration Manager Deployment
Microsoft Configuration Manager: An integrated solution for for managing large groups of personal computers and servers.Deployment: The process of delivering, assembling, and maintaining a particular version of a software system at a site.
906 questions
Microsoft Configuration Manager
{count} votes

Accepted answer
  1. Amandayou-MSFT 11,046 Reputation points
    2022-05-06T05:14:37.863+00:00

    Hi @Kedar Tamboli ,

    Mathematical operations are not supported in WMI, so FREESPACE/SIZE <= '0.1' can not be used.

    We could use wbemtest to see if the WMI query is correct. Here is the result of this query:

    199481-56.png

    So we could use powershell script to achieve it. Here is the powershell, kindly refer to it:

    $DiskCount = ((Get-WmiObject -Class Win32_DiskDrive).Caption).count  
    $DiskInfo = Get-WmiObject -Class Win32_LogicalDisk   
    echo "--------------------Statistics disk partition status-------------------------"  
    echo "    Drive letter    Free space   Remaining proportions"          
    foreach ($Drivers in $DiskInfo)   
    {  
        $PartitionID = $Drivers.DeviceID  
            if ($PartitionID -eq 'c:'){  
                $PartitionSize = "{0:N2}GB" -f ($Drivers.Size/1GB)  
                $PartitionFreeSize = "{0:N2}GB" -f ($Drivers.FreeSpace/1GB)  
                $PartitionFree = ($PartitionFreeSize/$PartitionSize)*100  
                    if(($PartitionFree -le 10) -or ($PartitionFreeSize/1 -lt 5GB)){  
                        echo "    $PartitionID       $PartitionFreeSize    $PartitionFree%"   
                     }  
            }  
    }  
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful