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.

  • TonyR

    I wanted to use this to create a hotkey to disable (and another hotkey to enable) my “TP-LINK Wireless N Adapter” (its name in Device Manager) but I get an error when running the script exactly as posted above:

    You must provide a value expression on the right-hand side of the ‘-eq’ operator.
    At C:TP-LINK.ps1:3 char:30
    + Where-Object {$_.Name -eq <<<< ""TP-LINK Wireless N Adapter""}
    + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ExpectedValueExpression

    Given I copied it AS-IS and I've only changed the name to the device manager name of the device, what am I doing wrong?

    • http://saulosilva.com/ Saulo

      Hi Tony,

      It seems that when I published this post, the blogging software encoded all quotes into &quot; (including the ones in the code). I have fixed that now; could you please ensure the line you pasted above matches exactly the one below?

      Where-Object {$_.Name -eq "TP-LINK Wireless N Adapter"}

      Let me know if that fixes it.

      • TonyR

        Ah, that’s better. But now I’m getting this – it doesn’t seem to know what disable or enable are: Disabling TP-LINK Wireless N Adapter… Method invocation failed because [System.Management.ManagementObject#rootcimv2Win32_NetworkAdapter] doesn’t contain
        a method named ‘Disable’.
        At F:~DataScriptsTP-LINK.ps1:7 char:27
        + $result = $adapter.Disable <<<< ()
        + CategoryInfo : InvalidOperation: (Disable:String) [], RuntimeException
        + FullyQualifiedErrorId : MethodNotFound

        Failed.
        Enabling TP-LINK Wireless N Adapter… Method invocation failed because [System.Management.ManagementObject#rootcimv2Win32_NetworkAdapter] doesn't contain
        a method named 'Enable'.
        At F:~DataScriptsTP-LINK.ps1:19 char:26
        + $result = $adapter.Enable <<<< ()
        + CategoryInfo : InvalidOperation: (Enable:String) [], RuntimeException
        + FullyQualifiedErrorId : MethodNotFound

        Failed.

      • TonyR

        OK – it seems that “…there’s no way of enabling /disabling Network adapters via WMI before Vista/2008″. I’m on XP Pro.

      • http://saulosilva.com/ Saulo

        Right. The class Win32_NetworkAdapter itself is available since Windows 2000, but unfortunately the Enable and Disable methods are only supported in Windows Vista and above.

        Here are the MSDN articles on Win32_NetworkAdapter
        and its Enable and Disable methods (see the “Requirements” section).

      • TonyR

        Bugger. Oh, well. Nevermind.