How can I check if system drive has less than 5GB using Powershell and WMI?

I need a Powershell and WMI query that will do the following:

  1. Doesn't assume that SystemDrive is C: (this needs to run on hundreds of machines and windows may not be on C)
  2. Checks if there is less than 5GB left on that drive
  3. Must be done in a Select query only in the form below
    1. Get-WmiObject -Query "Select * From Win32...."

Basically I want to check that the system drive on many computers have enough space left before I apply an update but I have to work with the constraints above.

 

Cheers!

  • There are only about a bazillion scripts out there that will gather this information for you.  Have you looked at any of the script repositories or Script Browser within the ISE?

    This is the script I use to get information about disks.

     001 
     002 
     003 
     004 
     005 
     006 
     007 
     008 
     009 
     010 
     011 
     012 
     013 
     014 
     015 
     016 
     017 
     018 
     019 
     020 
     021 
     022 
     023 
     024 
     025 
     026 
     027 
     028 
     029 
     030 
     031 
     032 
     033 
     034 
     035 
     036 
     037 
     038 
     039 
     040 
     041 
     042 
     043 
     044 
     045 
     046 
     047 
    #requires -version 3
    <#
    .SYNOPSIS
    Get-DiskInventory retrieves logical disk information from one or
    more computers.
    .DESCRIPTION
    Get-DiskInventory uses WMI to retrieve the Win32_LogicalDisk
    instances from one or more computers. It displays each disk's
    drive letter, free space, total size, and percentage of free
    space.
    .PARAMETER computername
    The computer name, or names, to query. Default: Localhost.
    .PARAMETER drivetype
    The drive type to query. See Win32_LogicalDisk documentation
    for values. 3 is a fixed disk, and is the default.
    .EXAMPLE
    Get-DiskInventory -computername SERVER-R2 -drivetype 3
    #>
    [CmdletBinding()]
    param (
        [Alias('HostName','cn')]
        [Parameter(ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True,Position=0)]
        [string[]]$ComputerName = "localhost",
     
        [ValidateSet(2,3)]
        [int]$DriveType = 3
    )
    BEGIN
    {
        Write-Verbose "Connecting to $($ComputerName)`nLooking for drive type $drivetype"
    }
    PROCESS
    {
        Get-WmiObject -Class Win32_LogicalDisk -ComputerName $ComputerName `
            -Filter "drivetype=$drivetype" |
        Sort-Object -Property DeviceID |
        Select-Object -Property @{label = 'ComputerName'; expression = {$_.SystemName}},
            DeviceID,
            @{label = 'FreeSpace(MB)'; expression = {$_.FreeSpace / 1MB -as [int]}},
            @{label = 'Size(GB)'; expression = {$_.Size / 1GB -as [int]}},
            @{label = '%Free'; expression = {$_.FreeSpace / $_.Size * 100 -as [int]}} |
            sort -Property ComputerName, DeviceID
    }
    END
    {
        Write-Verbose "Finis!"
    }
  • The first thing is to determine which drive is the system drive. Try this:

    $sysdrive = (Get-WmiObject Win32_operatingSystem).SystemDrive

    $free = (Get-WmiObject Win32_logicaldisk | where DeviceID -eq $sysdrive).FreeSpace

     

  • You should use WMI filtering for best performance.

    $OS = Get-WMiobject -Class Win32_operatingsystem
    Get-WMIObject Win32_Logicaldisk -filter "deviceid='$($os.systemdrive)'"

    I'm curious as to original request to use a Select query. What I have above is essentially the same thing.