PowerTip: List Physical Drives with PowerShell

PowerTip: List Physical Drives with PowerShell

  • Comments 12
  • Likes

Summary: Use Windows PowerShell to list physical drives.

Hey, Scripting Guy! Question How can I use Windows PowerShell to get a list of physical drives?

Hey, Scripting Guy! Answer Use Get-WMIObject, query win32_logicaldisk, and filter with the DriveType property:

GET-WMIOBJECT –query “SELECT * from win32_logicaldisk where DriveType = ‘3’”

To check the drives on a remote computer, add the –ComputerName parameter.
For example, to list all physical drives and their free space on a machine called “ContosoWS1”:

GET-WMIOBJECT –query “SELECT * from win32_logicaldisk where DriveType = ‘3’”
–computername “ContosoWS1”

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • For me, on Windows 8 Enterprise, i don't need the ' around the drivetype.

    These commandlets are OK for me:

    GET-WMIOBJECT –query “SELECT * from win32_logicaldisk where DriveType = 3”

    GET-WMIOBJECT –query “SELECT * from win32_logicaldisk where DriveType = 3”

    –computername “ContosoWS1”

  • @Christophe

    Actually it works that way in Windows 7 as well.  It's just a force of habit I have when working with WMI queries to isolate the results in the query.

    Love that you're rocking it with PowerShell!

    Sean

  • Strictly speaking, this command doesn't list physical drives at all.  It lists logical drives located on whatever Windows considers to be a "Local Disk" (which, oddly enough, includes some external USB drives, even though you might think those should be categorized as Removable).

    There can be many Win32_LogicalDisk objects associated with a single physical disk, if you have multiple partitions or volumes on that disk.

  • Quote are always soptional aroung numeric and Booleans.

    Prop1=True

    Prop1='True'

    Prop2=1

    Prop2='1'

    Strings always require quotes.  This is also true in almost all programming systems including VBScript and PowerShell.

  • @David

    You are correct in that it accesses the logical drive letters.   If you wish to access the PhysicalDisks leverage the win32_diskdrive within WMI.  The advantage I find with LogicalDisk is the ability to get the drive letter.

    @Jrv

    Good tip on working with WMI Queries :)

    Sean

  • Caution:  the snippet contains "smart" quotes so can't be literally copy-pasted.  You need to edit the apos and quot chars after ^C  ^V

  • Get-PSDrive

  • I am trying to get a list which contains drives of multiple servers and I wrote a script. But it looks like it is not taking multiple computers. Script is below get-WmiObject win32_logicaldisk -ComputerName (get-content d:\scripts\sea.txt) | Format-table -Property @{name='Free space(in GB)'; e={$_.Freespace/1GB}; align='right'; formatstring='N2'}, @{name='Total Size (in GB)'; e={$_.size/1GB}; align='right'; formatstring='N2'} -autosize >>D:\Scripts\Test.csv Please suggest alternatives.

  • Hi, I hope you can help. I need a good way to show the mapping between a iscsi lun to disk
    If I run Get-iSCSISession | Get-Disk | where {$_.operationalstatus -eq "online"} | Ft -autosize -wrap, I can see the disk number but the name isn't detailed.
    Number Friendly Name OperationalStatus Total Size Partition Style
    15 LEFTHAND iSCSIDisk Multi-Path Disk Device Online 123 GB MBR

    I need the "TargetNodeAddress : iqn.2003-10.com.lefthandnetworks:avalonprod:71724:on-dc01-av" with the Lun or disk ID

    Any help is awesome!

  • I can not recollect ever having working code from the scripting guys

  • @random IT guy: Totally agree. The example given should have been:

    Get-WmiObject -Class Win32_LogicalDisk | Where { $_.DriveType -eq 3 }

  • This is one of the preferred ways of filtering with Get-WMIObject using -Query:
    Get-WMIObject -Query "Select * FROM Win32_LogicalDisk WHERE DriveType=3"

    You could also use -Filter as well if desired:
    Get-WMIObject -Class Win32_logicaldisk -Filter "DriveType='3'"

    This applies the filter on the computer that it is being run against prior to sending the data back to PowerShell rather than bringing everything back to then be filtered by PowerShell.

    Try both of these out to see the difference between filtering using a parameter and using Where-Object:

    #Using the Query Parameter to filter:
    Get-WMIObject -Query "Select * FROM Win32_LogicalDisk WHERE DriveType=3" | Where {
    Write-Verbose $_ -Verbose
    }

    #Piping data into Where-Object to filter:
    Get-WMIObject -Class Win32_logicaldisk | Where {
    Write-Verbose $_ -Verbose
    $_.DriveType -eq 3
    }