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 the ways 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)}}

5 thoughts on “Windows PowerShell Technique: Get Hard Drive Information

  1. I would like to thank you for the efforts you have put in writing this blog. I’m hoping the same high-grade site post from you in the upcoming as well. In fact your creative writing abilities has encouraged me to get my own website now. Actually the blogging is spreading its wings quickly. Your write up is a great example of it.

  2. Hiya, I’m really glad I have found this info. Nowadays bloggers publish just about gossips and net and this is really frustrating. A good site with exciting content, that is what I need. Thanks for keeping this web site, I will be visiting it. Do you do newsletters? Can’t find it.

  3. I’m happy I found this site! From time to time, students want to cognitive the keys of productive literary essays composing. Your first-class knowledge about this good post can become a proper basis for such people. cheers!

Leave a Reply

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