2

In PowerShell, after I type "pnputil.exe -e", I get a list of driver info as below. I need to get class monitor's published name from the list and store "oem8.inf" into $monitor_name. Which command can I use to search for class = monitor and extract it's published name to a variable. Because the attribute "published name" has space, so I have no idea how to make this work.

Published name :            oem8.inf    
Driver package provider :   HP
Class :                     Monitors
Driver date and version :   07/25/2013 2.2.0.0    
Signer name :               Microsoft Windows Hardware Compatibility Publisher

Published name :            oem6.inf
Driver package provider :   Canon
Class :                     Printers
Driver date and version :   06/21/2006 6.1.7600.16385
Signer name :               Microsoft Windows

Published name :            oem1.inf
Driver package provider :   Microsoft
Class :                     Printers
Driver date and version :   06/21/2006 10.0.14393.0
Signer name :               Microsoft Windows
mklement0
  • 382,024
  • 64
  • 607
  • 775
John
  • 315
  • 3
  • 6
  • 12

3 Answers3

3

You can do this using WMI, via the Get-WMIObject cmdlet (alias gwmi):

gwmi Win32_PnPSignedDriver | ? DeviceClass -eq "MONITOR"

will return some details, you can narrow it down to the InfName using this:

gwmi win32_PnPSignedDriver | ? DeviceClass -eq "MONITOR" | Select InfName

InfName
-------
oem30.inf
oem30.inf
mklement0
  • 382,024
  • 64
  • 607
  • 775
colsw
  • 3,216
  • 1
  • 14
  • 28
3

colsw's helpful answer is the way to go and his approach illustrates an important point:

If you perform your task via PowerShell cmdlets such as Get-CimInstance (which supersedes the obsolete Get-WmiObject), there is no need for string manipulation in order to extract information - PowerShell's object-oriented nature allows for solutions that are both more robust and more convenient compared to traditional output-text-parsing techniques.

That said, if needed, PowerShell has powerful string parsing features too:

$monitor_name = pnputil.exe -e | 
                Select-String -Context 2 'Class :\s+ Monitors' |
                ForEach-Object { ($_.Context.PreContext[0] -split ' : +')[1] }

With your sample input, this yields:

oem8.inf
mklement0
  • 382,024
  • 64
  • 607
  • 775
1

Tagging on to mklement0 helpful answer as well. If you ever need all the object data for only monitors, you can do this.

Clear-Host
(PNPUtil /Enum-Drivers /class Display | 
Select-Object -Skip 2) | 
Select-String -Pattern 'Monitors' -Context 3,4
# Results
<#
  Published Name:     oe...
  Original Name:      tpl...
  Provider Name:      L...
> Class Name:         Monitors
  Class GUID:         {4d36e...
  Driver Version:     11/...
  Signer Name:        Microsof...

  Published Name:     oem...
  Original Name:      tp...
  Provider Name:      L....
> Class Name:         Monitors
  Class GUID:         {4d36...
  Driver Version:     06/...
  Signer Name:        Microsof...
#>

The cherry-pick as needed, or, create and use customobject to get your properties the same way, and then just select. For example:

Clear-Host
((PNPUtil /Enum-Drivers /class Display | 
Select-Object -Skip 2) | 
Select-String -Pattern 'Class Name:' -Context 3,4) | 
ForEach {
    [PSCustomObject]@{
        PublishedName = $PSItem.Context.PreContext[0] -replace '.*:\s+'
        OriginalName  = $PSItem.Context.PreContext[1] -replace '.*:\s+'
        ProviderName  = $PSItem.Context.PreContext[2] -replace '.*:\s+'
        ClassName     = ($PSitem | Select-String -Pattern 'Class Name:') -replace '.*:\s+'
        ClassGUID     = $PSItem.Context.PostContext[0] -replace '.*:\s+'
        DriverVersion = $PSItem.Context.PostContext[1] -replace '.*:\s+'
        SignerName    = $PSItem.Context.PostContext[2] -replace '.*:\s+'   
    }
}
# Results
<#
PublishedName : oe...
OriginalName  : amd...
ProviderName  : AMD
ClassName     : Sys...s
ClassGUID     : {4d...
DriverVersion : 02/...
SignerName    : Microsof...
...
PublishedName : oem4...
OriginalName  : w...
ProviderName  : West...
ClassName     : WD D...
ClassGUID     : {8496e8....
DriverVersion : 11...
SignerName    : Microsoft W...
#>

Or table format, or save to csv.

Clear-Host
((PNPUtil /Enum-Drivers /class Display | 
Select-Object -Skip 2) | 
Select-String -Pattern 'Class Name:' -Context 3,4) | 
ForEach {
    [PSCustomObject]@{
        PublishedName = $PSItem.Context.PreContext[0] -replace '.*:\s+'
        OriginalName  = $PSItem.Context.PreContext[1] -replace '.*:\s+'
        ProviderName  = $PSItem.Context.PreContext[2] -replace '.*:\s+'
        ClassName     = ($PSitem | Select-String -Pattern 'Class Name:') -replace '.*:\s+'
        ClassGUID     = $PSItem.Context.PostContext[0] -replace '.*:\s+'
        DriverVersion = $PSItem.Context.PostContext[1] -replace '.*:\s+'
        SignerName    = $PSItem.Context.PostContext[2] -replace '.*:\s+'   
    }
} | 
Format-Table -AutoSize
# Results
<#
PublishedName OriginalName   ProviderName                   ClassName                         ClassGUID                              DriverVersion         SignerName                        
------------- ------------   ------------                   ---------                         ---------                              -------------         ----------                        
...

Clear-Host
((PNPUtil /Enum-Drivers /class Display | 
Select-Object -Skip 2) | 
Select-String -Pattern 'Class Name:' -Context 3,4) | 
ForEach {
    [PSCustomObject]@{
        PublishedName = $PSItem.Context.PreContext[0] -replace '.*:\s+'
        OriginalName  = $PSItem.Context.PreContext[1] -replace '.*:\s+'
        ProviderName  = $PSItem.Context.PreContext[2] -replace '.*:\s+'
        ClassName     = ($PSitem | Select-String -Pattern 'Class Name:') -replace '.*:\s+'
        ClassGUID     = $PSItem.Context.PostContext[0] -replace '.*:\s+'
        DriverVersion = $PSItem.Context.PostContext[1] -replace '.*:\s+'
        SignerName    = $PSItem.Context.PostContext[2] -replace '.*:\s+'   
    }
} | 
Where-Object -Property ClassName -EQ 'Monitors' |
Select-Object -Property PublishedName
# Results
<#
PublishedName
-------------
oem11.inf    
oem67.inf 
#>
postanote
  • 15,138
  • 2
  • 14
  • 25