3

I run automation that installs a preset OS /w Drivers and configuration. I inherited the automation second hand and there's a lot to it. I have a system with hybrid graphics and it hands on verifying the display drivers. Now, I can walk over to the machine, open device manager, right click on one of the graphics devices, click uninstall (i do not delete the driver files) and then the automation continues. Once its done, the system restarts and both device drivers are back. I am wondering if there is a powershell command i can run to do the same task through automation?

what do you guys think!

ndocds
  • 33
  • 1
  • 1
  • 3

4 Answers4

4

You can do this using WMI instance:

get-wmiobject -Query "select * from win32_systemdriver where caption=`"THING_TO_REMOVE`"" } | ForEach  { $_.StopService()
$_.Delete()
 } 
Wasif
  • 14,755
  • 3
  • 14
  • 34
2
foreach ($dev in (Get-PnpDevice | Where-Object {
  $_.Name -like "*Name of device to remove*")) {
    &"pnputil" /remove-device $dev.InstanceId; &"pnputil" /scan-devices
  }
}

You can use this script in a UninstallAndRescan.ps1 file and execute it at startup using Task Scheduler if you need to execute it on every reboot as necessary.

  • please provide a working example. incomplete code-snippets are not really useful. – snahl Sep 01 '22 at 19:03
  • 1
    You're missing a curly brace closing the where-object clause. Otherwise it works ok to fix the error prone "Intel(R) UHD Graphics 630" driver. – js2010 Sep 17 '22 at 01:18
0

It's not powershell, but I've always used the command line version of device manager called devcon: https://learn.microsoft.com/en-us/windows-hardware/drivers/devtest/devcon

Simplified version of another answer:

# dell2020vidfix.ps1

# Get-PnpDevice -PresentOnly -Status ERROR,DEGRADED,UNKNOWN

foreach ($dev in Get-PnpDevice -FriendlyName '*Intel(R) UHD Graphics 630*') {
  pnputil /remove-device $dev.InstanceId }
pnputil /scan-devices
js2010
  • 23,033
  • 6
  • 64
  • 66
0
    #Set-ExecutionPolicy RemoteSigned

Get-PnpDevice -status unknown |Select-Object -Property FriendlyName, class, InstanceId, HardwareID | Where-Object {$_.class -like 'SmartCardReader'  -and $_.FriendlyName -match 'WUDF'} 
Get-PnpDevice -status unknown |Select-Object -Property FriendlyName, class, InstanceId, HardwareID | Where-Object {$_.class -like 'USB'  -and $_.FriendlyName -match 'Rutoken'} 
Get-PnpDevice -status unknown |Select-Object -Property FriendlyName, class, InstanceId, HardwareID | Where-Object {$_.class -like 'USB'  -and $_.FriendlyName -match 'устройство'} 
Get-PnpDevice -status unknown |Select-Object -Property FriendlyName, class, InstanceId, HardwareID | Where-Object {$_.class -like 'USB'  -and $_.FriendlyName -match 'концентратор'} 
Get-PnpDevice -status unknown |Select-Object -Property FriendlyName, class, InstanceId, HardwareID | Where-Object {$_.class -like 'SmartCardFilter'  -and $_.FriendlyName -match 'Драйвер фильтра смарт-карты'} 
Get-PnpDevice -status unknown |Select-Object -Property FriendlyName, class, InstanceId, HardwareID | Where-Object {$_.class -like 'HIDClass'  -and $_.FriendlyName -match 'HID'} 
Get-PnpDevice -status unknown |Select-Object -Property FriendlyName, class, InstanceId, HardwareID | Where-Object {$_.class -like 'HIDClass'  -and $_.FriendlyName -match 'USB-устройство ввода'} 
Get-PnpDevice -status unknown |Select-Object -Property FriendlyName, class, InstanceId, HardwareID | Where-Object {$_.class -like 'Volume'  -and $_.FriendlyName -match 'Том'} 
Get-PnpDevice -status unknown |Select-Object -Property FriendlyName, class, InstanceId, HardwareID | Where-Object {$_.class -like 'WPD'} 
pause

foreach ($dev in Get-PnpDevice -status unknown |Select-Object -Property FriendlyName, class, InstanceId, HardwareID | Where-Object {$_.class -like 'SmartCardReader'  -and $_.FriendlyName -match 'WUDF'}  ) {pnputil /remove-device $dev.InstanceId }
foreach ($dev in Get-PnpDevice -status unknown |Select-Object -Property FriendlyName, class, InstanceId, HardwareID | Where-Object {$_.class -like 'USB'  -and $_.FriendlyName -match 'Rutoken'}  ) {pnputil /remove-device $dev.InstanceId }
foreach ($dev in Get-PnpDevice -status unknown |Select-Object -Property FriendlyName, class, InstanceId, HardwareID | Where-Object {$_.class -like 'USB'  -and $_.FriendlyName -match 'устройство'}  ) {pnputil /remove-device $dev.InstanceId }
foreach ($dev in Get-PnpDevice -status unknown |Select-Object -Property FriendlyName, class, InstanceId, HardwareID | Where-Object {$_.class -like 'USB'  -and $_.FriendlyName -match 'концентратор'}  ) {pnputil /remove-device $dev.InstanceId }
foreach ($dev in Get-PnpDevice -status unknown |Select-Object -Property FriendlyName, class, InstanceId, HardwareID | Where-Object {$_.class -like 'SmartCardFilter'  -and $_.FriendlyName -match 'Драйвер фильтра смарт-карты'}  ) {pnputil /remove-device $dev.InstanceId }
foreach ($dev in Get-PnpDevice -status unknown |Select-Object -Property FriendlyName, class, InstanceId, HardwareID | Where-Object {$_.class -like 'HIDClass'  -and $_.FriendlyName -match 'HID'}  ) {pnputil /remove-device $dev.InstanceId }
foreach ($dev in Get-PnpDevice -status unknown |Select-Object -Property FriendlyName, class, InstanceId, HardwareID | Where-Object {$_.class -like 'HIDClass'  -and $_.FriendlyName -match 'USB-устройство ввода'}  ) {pnputil /remove-device $dev.InstanceId }
foreach ($dev in Get-PnpDevice -status unknown |Select-Object -Property FriendlyName, class, InstanceId, HardwareID | Where-Object {$_.class -like 'Volume'  -and $_.FriendlyName -match 'Том'}  ) {pnputil /remove-device $dev.InstanceId }
foreach ($dev in Get-PnpDevice -status unknown |Select-Object -Property FriendlyName, class, InstanceId, HardwareID | Where-Object {$_.class -like 'WPD'}  ) {pnputil /remove-device $dev.InstanceId }

pnputil /scan-devices
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 24 '23 at 09:04
  • Hello, please don't just submit code in your answer(s), add some details as to why you think this is the optimal solution. – Destroy666 May 25 '23 at 14:42