🧬 WQL / PowerShell

Win32_LogicalDisk: Sorting Logical Disks (WQL has no ORDER BY)

WQL has no ORDER BY clause — this shows the standard workaround of piping Get-CimInstance results through Sort-Object on Win32_LogicalDisk.

By WindowsScripting.com · 651 B
LogicalDiskSorted.ps1
<#
.SYNOPSIS
    WQL reference: sorting Logical Disks results (Win32_LogicalDisk).

.DESCRIPTION
    WQL (the WMI Query Language) does not support an ORDER BY clause —
    unlike regular SQL. Sort after the fact with Sort-Object instead;
    this is one of the most common WQL gotchas for people coming from
    SQL backgrounds.

.EXAMPLE
    .\LogicalDiskSorted.ps1
#>

[CmdletBinding()]
param()

$wqlQuery = "SELECT DeviceID, VolumeName, Size, FreeSpace, DriveType FROM Win32_LogicalDisk"

Get-CimInstance -Query $wqlQuery |
    Sort-Object -Property DeviceID |
    Format-Table -Property DeviceID, VolumeName, Size, FreeSpace, DriveType -AutoSize