Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uninstall Device from powershell

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!

like image 717
ndocds Avatar asked Sep 17 '26 07:09

ndocds


2 Answers

You can do this using WMI instance:

Get-CimInstance -Query (@"
select * from win32_systemdriver where caption="{0}"
"@ -f "THING_TO_REMOVE") |
    ForEach-Object {
        $_.StopService()
        $_.Delete()
    }
like image 150
programmer365 Avatar answered Sep 20 '26 02:09

programmer365


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.

like image 35
Muhammad Asif Mohtesham Avatar answered Sep 20 '26 02:09

Muhammad Asif Mohtesham