stackprotector
stackprotector

Reputation: 13412

How do I get the first install date of a disk drive in PowerShell?

In the Device Manager, I can view the properties of any device (disk drives included). In the Details tab, I can select the first install date:

enter image description here

In PowerShell, I can get all disk drives by issuing:

Get-CimInstance -ClassName CIM_DiskDrive

The returned objects have an InstallDate property, but it is empty. How can I get the date, that is visible in the device manager, in PowerShell? Do I have to associate the CIM_DiskDrive class with another CIM class? If so, which?

Upvotes: 1

Views: 1088

Answers (2)

stackprotector
stackprotector

Reputation: 13412

With the excellent pointer from Almighty's answer I came up with the following solution:

Get-PnpDevice -Class DiskDrive -Status OK | ForEach-Object {
    [PSCustomObject]@{
        FriendlyName=(Get-PnpDeviceProperty -InputObject $_ -KeyName DEVPKEY_Device_FriendlyName).Data;
        FirstInstallDate=(Get-PnpDeviceProperty -InputObject $_ -KeyName DEVPKEY_Device_FirstInstallDate).Data
    }
}

It gets all currently installed disk drives and returns their friendly names and first install dates as a custom object for further processing.

Upvotes: 1

Almighty
Almighty

Reputation: 178

Using Get-PnpDeviceProperty you can pull the additonal info that you're looking for. It is not as intuitive as you would think, but it does work! I am sure you can find the exact Class and pull it directly, but this is a functional way nonetheless.

# Get PNPDeviceID from the disk driver
$PNPDeviceID = Get-CimInstance -ClassName CIM_DiskDrive | Select-Object -ExpandProperty PNPDeviceID

# Get friendly name to verify and first install date
Get-PnpDeviceProperty -InstanceId $PNPDeviceID -KeyName DEVPKEY_Device_FriendlyName, DEVPKEY_Device_FirstInstallDate | Select-Object KeyName, Data

Upvotes: 1

Related Questions