Use PowerShell to Get Drive List and Capacity
Use PowerShell to Get Drive List and Capacity
Introduction
Just a simple post with PowerShell. Had a bunch of Windows servers that needed to get a list of local drives and display the size of the drive and the freespace on the drives in GB. To achieve that decided to use Win32_LogicalDisk call in PowerShell. select a few of the available fields and do a little math.
The Code
What is Win32_LogicalDisk? The Win32_LogicalDisk WMI class represents a data source that resolves to an actual local storage device on a computer system running Windows. Details on the class and properties can be found here. For quick and dirty line of code we will want to display the properties.
- DeviceID
- VolumeName
- Size
- FreeSpace
Get all Logical Disk PowerShell Code
gwmi win32_logicaldisk | select DeviceID, VolumeName, @{Name=”Capacity(GB)”;expression={[math]::round(($_.Size/ 1073741824),2)}}, @{Name=”FreeSpace(GB)”;expression={[math]::round(($_.FreeSpace / 1073741824),2)}}
Output
What are we doing?
First part of the line we want to call the WMI Class, we do this with gwmi or Get-WmiObject and call the class. gwmi win32_logicaldisk. Now we add the pipe symbol | and select and format our output.
select DeviceID, VolumeName, @{Name=”Capacity(GB)”;expression={[math]::round(($_.Size/ 1073741824),2)}}, @{Name=”FreeSpace(GB)”;expression={[math]::round(($_.FreeSpace / 1073741824),2)}}
For the size and freespace we will need to define the column name and use expression to handle the math functions. If we want to add more properties to display just add a column and add the object name and any formatting / expressions.
Leave a Reply