How to disable & enable a network adapter on Windows with PowerShell

Sometimes my USB wireless network adapter doesn’t work at all after Windows 7 recovers from sleep mode. I haven’t found the root cause of this problem but disabling & enabling the adapter works well as a workaround. Here is a PowerShell script to do so:

# Get the network adapter object
$adapter = Get-WmiObject -Class Win32_NetworkAdapter |
    Where-Object {$_.Name -eq "TP-LINK Wireless USB Adapter"}

# Disable it
Write-Host -nonew "Disabling $($adapter.Name)... ";
$result = $adapter.Disable()
if ($result.ReturnValue -eq -0) {
    Write-Host "Success.";
} else {
    Write-Host "Failed.";
}

# Wait 2 seconds
Start-Sleep -s 2

# Enable it
Write-Host -nonew "Enabling $($adapter.Name)... ";
$result = $adapter.Enable()
if ($result.ReturnValue -eq -0) {
    Write-Host "Success.";
} else {
    Write-Host "Failed.";
}

Make sure the adapter name on line 3 matches what you have in Control Panel > Network and Internet > Network Connections. Mine is called “TP-LINK Wireless USB Adapter”.

Since you will need to run that as an Administrator, create a shortcut to the PowerShell executable and pass the full path of the script above as an argument. Then, open the shortcut properties, click on “Advanced…” and check “Run as an administrator”.

Update: This method only works on Windows Vista and above.

Update 2: I have not encountered this issue since I’ve updated to Windows 8.