powershellcmdpowershell-2.0powershell-3.0powershell-4.0

How can I obtain the driver key for an installed device?


Unfortunately this Question was asked 4 years ago but no answer yet How can I obtain the driver key for a driver programmatically?

actually if it was answered, I would not ask again.

I have a software depends on Naps2 scanner software. I want to generate profiles.xml file and it depends on Driver Key and DeviceName

profiles.xml like below:

<?xml version="1.0"?>
<ArrayOfScanProfile xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <ScanProfile>
    <Device>
      <ID>{6BDD1FC6-810F-11D0-BEC7-08002BE2092F}\0000</ID>
      <Name>HP Scanjet 300</Name>
    </Device>
    <DriverName>wia</DriverName>
    <DisplayName>HP Scanjet 300</DisplayName>
    <IconID>0</IconID>
    <MaxQuality>false</MaxQuality>
    <IsDefault>true</IsDefault>
    <Version>2</Version>
    <UseNativeUI>false</UseNativeUI>
    <AfterScanScale>OneToOne</AfterScanScale>
    <Brightness>0</Brightness>
    <Contrast>0</Contrast>
    <BitDepth>C24Bit</BitDepth>
    <PageAlign>Right</PageAlign>
    <PageSize>Letter</PageSize>
    <Resolution>Dpi200</Resolution>
    <PaperSource>Glass</PaperSource>
    <EnableAutoSave>false</EnableAutoSave>
    <Quality>75</Quality>
    <AutoDeskew>false</AutoDeskew>
    <BrightnessContrastAfterScan>false</BrightnessContrastAfterScan>
    <ForcePageSize>false</ForcePageSize>
    <ForcePageSizeCrop>false</ForcePageSizeCrop>
    <TwainImpl>Default</TwainImpl>
    <ExcludeBlankPages>false</ExcludeBlankPages>
    <BlankPageWhiteThreshold>70</BlankPageWhiteThreshold>
    <BlankPageCoverageThreshold>25</BlankPageCoverageThreshold>
    <WiaOffsetWidth>false</WiaOffsetWidth>
    <WiaRetryOnFailure>false</WiaRetryOnFailure>
    <WiaDelayBetweenScans>false</WiaDelayBetweenScans>
    <WiaDelayBetweenScansSeconds>2</WiaDelayBetweenScansSeconds>
    <WiaVersion>Default</WiaVersion>
    <FlipDuplexedPages>false</FlipDuplexedPages>
  </ScanProfile>
  <ScanProfile>
    <Device>
      <ID>HP Scanjet 300 TWAIN</ID>
      <Name>HP Scanjet 300 TWAIN</Name>
    </Device>
    <DriverName>twain</DriverName>
    <DisplayName>HP Scanjet 300 TWAIN</DisplayName>
    <IconID>0</IconID>
    <MaxQuality>false</MaxQuality>
    <IsDefault>false</IsDefault>
    <Version>2</Version>
    <UseNativeUI>false</UseNativeUI>
    <AfterScanScale>OneToOne</AfterScanScale>
    <Brightness>0</Brightness>
    <Contrast>0</Contrast>
    <BitDepth>C24Bit</BitDepth>
    <PageAlign>Right</PageAlign>
    <PageSize>Letter</PageSize>
    <Resolution>Dpi200</Resolution>
    <PaperSource>Glass</PaperSource>
    <EnableAutoSave>false</EnableAutoSave>
    <Quality>75</Quality>
    <AutoDeskew>false</AutoDeskew>
    <BrightnessContrastAfterScan>false</BrightnessContrastAfterScan>
    <ForcePageSize>false</ForcePageSize>
    <ForcePageSizeCrop>false</ForcePageSizeCrop>
    <TwainImpl>Default</TwainImpl>
    <ExcludeBlankPages>false</ExcludeBlankPages>
    <BlankPageWhiteThreshold>70</BlankPageWhiteThreshold>
    <BlankPageCoverageThreshold>25</BlankPageCoverageThreshold>
    <WiaOffsetWidth>false</WiaOffsetWidth>
    <WiaRetryOnFailure>false</WiaRetryOnFailure>
    <WiaDelayBetweenScans>false</WiaDelayBetweenScans>
    <WiaDelayBetweenScansSeconds>2</WiaDelayBetweenScansSeconds>
    <WiaVersion>Default</WiaVersion>
    <FlipDuplexedPages>false</FlipDuplexedPages>
  </ScanProfile>
</ArrayOfScanProfile>

after searching windows devices I found that the Driver Key {6BDD1FC6-810F-11D0-BEC7-08002BE2092F}\0000 is in here:

enter image description here

What I need is a PowerShell script or one-line script that gives me a Driver Key like "{6BDD1FC6-810F-11D0-BEC7-08002BE2092F}\0000" with zeros or any number depending on how much scanners are connected to the machine (NOT Class GUID only {6BDD1FC6-810F-11D0-BEC7-08002BE2092F}), and Device Name like below:

Get-WmiObject Win32_PNPEntity | Where-Object { $_.PNPCLass -eq "Image"} | select *

Or

Get-WmiObject Win32_PnPSignedDriver | Where-Object { $_.PNPCLass -eq "Image" } | select *

Thanks in advance


Solution

  • Removing all the stuff from my first response (if you kept that, it may be useful later to you) and coming back to what Abraham Zinala pointed you to and tweak it to get the value you are after.

    # Using WMI
    Get-WmiObject -Class win32_pnpEntity
    
    Get-WmiObject -Class win32_pnpEntity -Filter 'Name like "Brother%"'
    
    (Get-WmiObject -Class win32_pnpEntity -Filter 'Name like "Brother%"')[1] | 
    Select-Object -Property '*'
    
    (Get-WmiObject -Class win32_pnpEntity -Filter 'Name like "Brother%"')[1].GetDeviceProperties().DeviceProperties  | 
    Select-Object -Property keyName, data | 
    Select-Object -First 7
    # Results
    <#
    keyName                      data                                        
    -------                      ----                                        
    DEVPKEY_Device_DeviceDesc    Local Print Queue                           
    DEVPKEY_Device_HardwareIds   {PRINTENUM\LocalPrintQueue}                 
    DEVPKEY_Device_CompatibleIds {GenPrintQueue, SWD\GenericRaw, SWD\Generic}
    DEVPKEY_Device_Class         PrintQueue                                  
    DEVPKEY_Device_ClassGuid     {1ED2BBF9-...}      
    DEVPKEY_Device_Driver        {1ed2bbf9-...}\0007 
    DEVPKEY_Device_ConfigFlags   0                                           
    ...
    #>
    
    
    ((Get-WmiObject -Class win32_pnpEntity -Filter 'Name like "Brother%"')[1].GetDeviceProperties().DeviceProperties).Data[7]
    # Results
    <#
    {1ed2bbf9-...}\0007
    #>
    
    
    # Using CIM
    Get-CimInstance -ClassName win32_pnpEntity
    
    Get-CimInstance -ClassName  win32_pnpEntity -Filter 'Name like "Brother%"'
    
    (Get-CimInstance -ClassName  win32_pnpEntity -Filter 'Name like "Brother%"')[1]
    
    (Get-CimInstance -ClassName  win32_pnpEntity -Filter 'Name like "Brother%"')[1] | 
    Select-Object -Property '*'
    
    # This however will fail
    (Get-CimInstance -ClassName  win32_pnpEntity -Filter 'Name like "Brother%"')[1].GetDeviceProperties().DeviceProperties  | 
    Select-Object -Property keyName, data | 
    Select-Object -First 7
    # Results
    <#
    Method invocation failed because [Microsoft.Management.Infrastructure.CimInstance] does not contain a method named 'GetDeviceProperties'
    #>
    
    # So, get the device Instance ID to pass to another cmdlet
    (Get-CimInstance -Namespace root/cimv2 -ClassName Win32_PNPEntity | 
    Where-Object PNPClass -like 'Print*')[2] | 
    Format-Table -AutoSize
    # Results
    <#
    Status Class      FriendlyName   InstanceId                                          
    ------ -----      ------------   ----------                                          
    OK     PrintQueue Brother...     SWD\PRINTENUM\...}
    
    #>
    
    ($DeviceInstanceID = ((Get-CimInstance -Namespace root/cimv2 -ClassName Win32_PNPEntity | 
                        Where-Object PNPClass -like 'Print*')[2]).InstanceId)
    # Results
    <#
    SWD\PRINTENUM\{0A235CEC-...}
    #>
    

    Point of note: Not all known cmdlets are available on all PS versions/OS platforms.

    I use my custom function to get such info dynamically as needed.

    Get-CmdletVersion -CmdLetName Get-PnpDeviceProperty
    # Results
    <#
    ModuleType Name      CmdletName_FinctionName PowerShellVersion
    ---------- ----      ----------------------- -----------------
                                                              
    Manifest   PnpDevice Get-PnpDeviceProperty   5.1 
    #>
    
    
    Get-PnpDeviceProperty -InstanceId $DeviceInstanceID
    # Results
    <#
    InstanceId KeyName                                   Type       Data                                                       
    ---------- -------                                   ----       ----                                                       
    SWD\PRI... DEVPKEY_Device_DeviceDesc                 String     Local Print Queue                                          
    SWD\PRI... DEVPKEY_Device_HardwareIds                StringList {PRINTENUM\LocalPrintQueue}                                
    SWD\PRI... DEVPKEY_Device_CompatibleIds              StringList {GenPrintQueue, SWD\GenericRaw, SWD\Generic}               
    SWD\PRI... DEVPKEY_Device_Class                      String     PrintQueue                                                 
    SWD\PRI... DEVPKEY_Device_ClassGuid                  Guid       {1ED2BBF9-...}                     
    SWD\PRI... DEVPKEY_Device_Driver                     String     {1ed2bbf9-...}\0007                
    ...
    #>
    
    (Get-PnpDeviceProperty -InstanceId $DeviceInstanceID)[5].Data
    # Results
    <#
    {1ed2bbf9-...}\0007
    #>