Windows PowerShell Technique: Get Hard Drive Information

Featured Programming Tips-And-Tricks

Windows PowerShell is a command-line shell for Microsoft Windows that is mostly used for system administration. It uses cmdlets (commandlets) that are .NET classes to perform simple to complex administrative tasks. It can access Component Object Model (COM) and Windows Management Instrumentation (WMI) for local, as well as remote administration. PowerShell can also be embedded within applications to leverage its capability to pull off useful information.

There’s a wealth of information on the internet about the details of Windows PowerShell, but here we’ll focus on a way to get hard drive and partitions information using Windows PowerShell in a concise manner.

In Windows operating systems there are lots of ways to get information about storage disks and partitions, for example, you can use the Disk Management utility with a graphical interface or the command-line utility diskpart.

A list of all cmdlets of the Storage module can be listed by running the below command under Windows PowerShell console:

Get-Command -Module Storage

As you can see from the image below, there are a myriad of commands (cmdlets) available for disk functions.

Let’s delve a little into it by playing around with a few of those cmdlets.

Get-PhysicalDisk allows you to get information about physical disks’ attributes like its serial number, media type, size etc.

Get-Disk display disk information at the logical level of the operating system.

Get-Partition show partition information of all drives; e.g, partition number, drive letter, size.

Get-Volume display volume information on all disks, e.g, drive letter, file system, size.

Let’s get our feet a little more wet and give some experiment to receive some more useful information. By using the Format-Table (ft) cmdlet, display the desired properties in a table.

For example, display the device id, model, media type, bus type, and size, run this command:

Get-PhysicalDisk | ft DeviceId,Model,MediaType,BusType,Size

As you can see the size is displayed in bytes which is affects readability. Let’s make it readable for us by adding a little math to display the size in Giga bytes. Give the command and see for yourself.

Get-PhysicalDisk | ft DeviceId,Model,MediaType,BusType, @{Name="Size (GB)"; Expression={[int]($_.Size/1Gb)}}

Not to mention that you can display the size in Mega Bytes as well by just making a little change in the above command.

Get-PhysicalDisk | ft DeviceId,Model,MediaType,BusType, @{Name="Size (MB)"; Expression={[int]($_.Size/1Mb)}}

To get available disk space you can use Get-CimInstance cmdlet and Win32_LogicalDisk class. Set DriveType of 3 — the value WMI uses for fixed hard disks.

Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3"

To get the size and free space in GB you have to use expression as below:

Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | ft DeviceId, VolumeName, @{Name="Size (GB)"; Expression={[int]($_.Size/1Gb)}}, @{Name="FreeSpace (GB)"; Expression={[int]($_.FreeSpace/1Gb)}}

Leave a Reply

Your email address will not be published. Required fields are marked *