How I wanted to make a PowerShell script that resets a driver

in maxhacks •  8 years ago  (edited)

My WiFi driver sometimes crashes in the morning when trying to connect to my school’s Eduroam network. At first I restarted my laptop and it solved the problem. I found out that it's possible to restart your driver in Device Manager, but that takes too much time to do (a whole minute). So I decided to write a script that would reset the driver in a click. You can use this method to restart any driver.

The method that didn’t work out for me:

At first I needed to install the Device Manager PowerShell Cmdlets
You can download it from here.

  1. Unzip it
  2. Start PowerShell
  3. CD to the directory
  4. Import-Module .\DeviceManagement.psd1 –Verbose

Now it’s possible to get a list of all devices by typing Get-Device

Find the name of the driver you want to reset and take a piece of the name that’s unique. In my case it’s Ultimate-N 6300.

With this command it’d be possible to disable my WLAN driver:
Get-Device | Where-Object -Property Name -Like 'Ultimate-N 6300' | Disable-Device
SetupDiCallClassError
Unfortunately I got an error. After spending some time on Google I couldn’t find any solution. I read that the error was probably due to a x64/x32 bit mismatch. After that I tried to find a different way.

The method that worked for me

  1. Download DevCon: 32bit / 64bit

  2. Place devcon.exe in c:\windows\system32
    (the \ia64\devcon.exe won’t work)

  3. Find the name of the driver you want to reset and take a piece of the name that’s unique.
    You can get a list of all devices by typing devcon find *
    It returned all devices including my WiFi adapter:
    PCI\VEN_8086&DEV_4238&SUBSYS_11118086&REV_35\0024D7FFFF1755E000: Intel(R) Centrino(R) Ultimate-N 6300 AGN

  4. devcon disable *DEV_4238
    devcon enable *DEV_4238
    That’s it!

Making it into a script

I found this piece of code, which makes PowerShell ask for Admin privileges (UAC thingy)
The last line closes the current PowerShell session.
param([switch]$Elevated)
function Test-Admin {
$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
$currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
if ((Test-Admin) -eq $false) {
if ($elevated)
{
# tried to elevate, did not work, aborting
}
else {
Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
}
exit
}
devcon disable *DEV_4238
devcon enable *DEV_4238
stop-process -Id $PID

Copy paste it in a text editor and save it as a .ps1 file.
Or download it here: restartDriver.ps1

Now you can double click the file to restart your driver(s) :)

Oh no!! ERROR
UnauthorizedAccess
To solve that you’ll have to type this line in PowerShell:
Set-ExecutionPolicy RemoteSigned

Thanks for reading!

This is one of my hobbies :P trying computer stuff!

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!