Search code examples
powershellselect-string

Powershell using Select-String gives no results


I have the following command

Get-WindowsDriver -Online -All |  where {$_.ClassName -like "Display"}

This gives me the result like this

Driver           : vrd.inf
OriginalFileName : C:\Windows\System32\DriverStore\FileRepository\vrd.inf_amd64_3a0ba97737bffd01\vrd.inf
Inbox            : True
ClassName        : Display
BootCritical     : False
ProviderName     : Microsoft
Date             : 6/21/2006 12:00:00 AM
Version          : 10.0.18362.329

Driver           : wvmbusvideo.inf
OriginalFileName : C:\Windows\System32\DriverStore\FileRepository\wvmbusvideo.inf_amd64_483a786e00a2cb7a\wvmbusvideo.inf
Inbox            : True
ClassName        : Display
BootCritical     : False
ProviderName     : Microsoft
Date             : 6/21/2006 12:00:00 AM
Version          : 10.0.18362.1

Now I want to extract only the OriginalFileName line of both results so my output would look like this

OriginalFileName : C:\Windows\System32\DriverStore\FileRepository\vrd.inf_amd64_3a0ba97737bffd01\vrd.inf
OriginalFileName : C:\Windows\System32\DriverStore\FileRepository\wvmbusvideo.inf_amd64_483a786e00a2cb7a\wvmbusvideo.inf

Now to obtain the above result i can do this

Get-WindowsDriver -Online -All |  where {$_.ClassName -like "Display"} | findstr OriginalFileName

but the problem with the above command is that findstr has a character limit from what I have read, because of that some of the paths are cut. So in order to resolve that I am trying to use Select-String. This is what I am doing

Get-WindowsDriver -Online -All |  where {$_.ClassName -like "Display"} | Select-String -Pattern "OriginalFileName" 

but I am not getting any results. Any suggestions on what I might be doing wrong ?


Solution

  • Since Get-WindowsDriver returns objects (Microsoft.Dism.Commands.BasicDriverObject and/or Microsoft.Dism.Commands.AdvancedDriverObject), you should not use Select-String to find a single property of these objects.

    Simply return them like

    (Get-WindowsDriver -Online -All |  Where-Object {$_.ClassName -like "Display*"}).OriginalFileName
    

    If you want the output to look like PropertyName: PropertyValue, you could use

    Get-WindowsDriver -Online -All |  Where-Object {$_.ClassName -like "Display*"} | 
                                      Select-Object OriginalFileName | Format-List