Wednesday, 1 April 2015

Use PowerShell to detect if a disk is an SSD

Update 2016-03-18: If you are on Windows 8 / Server 2012 or later, use the 'MediaType' attribute of the 'Get-PhysicalDisk' output. See the comments for more detail.

I was talking with some colleagues recently who were having trouble detecting if a disk was a Solid State Disk (SSD) or not in a lab environment. They wanted to run PowerShell scripts against the machines but found if the disk on the machine was a spindle disk it would cause an issue without a built-in delay. They wanted to detect if the disk was an SSD to remove the delay on machines that could handle it.

Easy I though. Just issue a Get-PhysicalDisk cmdlet and checkout the properties, no problem. Well that's what a PowerShell expert would think anyway. It turns out not to be the case.

After a lot of mucking about and searching I found there is no information reported from an ATA device to tell the operating system that it is an SSD. This makes sense I suppose seeing the concept of an SSD would have been unimaginable back when the interfaces were designed.

It turns out the best way to detect if an attached disk is an SSD or not is by reading two properties of the disk being, spindle speed (or rotation rate) and seek penalty. Neither of these properties are exposed by PowerShell or .NET.

Thanks to the Add-Type cmdlet and a guy from this blog (sorry, I can't find your name on the site), I created this PowerShell script to report if a disk is an SSD or not.

Simply copy the code to a script file and run it passing in a physical disk id number. It will return $true if the disk is an SSD, or $false if not. It will also return $false if exceptions occur in the Win32 DLL calls.