Enabling and disabling network adapters

    PowerTip : Enable all network adapters

    Question: You are troubleshooting your Windows 8.1 laptop and want to quickly enable all network adapters. How can you do this?

    Answer: Use the Get-NetAdapter and the Enable-NetAdapter commands. The command line appears here:

    In the old days, back before Windows Vista and Windows Server 2008 when I needed to enable or disable a network adapter, I would actually use . Devcon is a command line utility that provides the ability to enable and to disable various hardware devices. It is billed as a command-line Device Manager. Here is a VBScript I wrote to enable and to disable the network interface adapter using Devcon. Keep in mind that Devcon is not installed by default, and therefore must be installed prior to use.

    1. '
    2. ' VBScript: AUTHOR: Ed Wilson , MS, 5/5/2004
    3. '
    4. ' NAME: <turnONoffNet.vbs>
    5. '
    6. ' COMMENT: Key concepts are listed below:
    7. '1.uses the c:\devcon utility to turn on or off net
    8. '2.uses a vbyesNO msgBOX to solicit input
    9. '3. KB 311272 talks about devcon and shows where to get
    10. '==========================================================================
    11. Option Explicit
    12. Dim objShell
    13. Dim objExec
    14. Dim onWireLess
    15. Dim onLoopBack
    16. Dim turnON
    17. Dim turnOFF
    18. Dim yesNO
    19. Dim message, msgTitle
    20. Dim strText
    21. message = "Turn On Wireless? Loop is disabled" & vbcrlf & "if not, then wireless is disabled and loop enabled"
    22. msgTitle = "change Network settings"
    23. onWireLess = " PCMCIA\Dell-0156-0002"
    24. onLoopBack = " \*loop"
    25. turnON = "enable"
    26. turnOFF = "disable"
    27. Const yes = 6
    28. Set objShell = CreateObject("wscript.shell")
    29. yesNO = MsgBox(message,vbyesNO,msgTitle)
    30. If yesNO = yes Then
    31. WScript.Echo "yes chosen"
    32. subOUT
    33. Set objExec = objShell.exec("cmd /c c:\devcon " & turnOFF & onLoopBack)
    34. subOUT
    35. Else
    36. WScript.Echo "no chosen"
    37. Set objExec = objShell.exec("cmd /c c:\devcon " & turnOFF & onWireLess)
    38. subOUT
    39. Set objExec = objShell.exec("cmd /c c:\devcon " & turnON & onLoopBack)
    40. subOUT
    41. End If
    42. Sub subOUT
    43. Do until objExec.StdOut.AtEndOfStream
    44. strText = objExec.StdOut.ReadLine()
    45. Wscript.Echo strText
    46. Loop
    47. End sub

    Beginning with Windows Vista (and Windows Server 2008) the Win32_NetworkAdapter class gains two methods: disable and enable. These are documented on MSDN here. These methods are instance methods which means that to use them, I need to first obtain an instance of the WMI class. What does this mean? Well I am using Win32_NetworkAdapter and therefore I am working with network adapters. So, I need to get a specific network adapter, and then I can disable it or enable it. Here is how it might work:

    1. $wmi=Get-WmiObject-ClassWin32\_NetworkAdapter-filter"Name LIKE '%Wireless%'"
    2. $wmi.disable()

    OR

    1. $wmi=Get-WmiObject-ClassWin32\_NetworkAdapter-filter"Name LIKE '%Wireless%'"
    2. $wmi.enable()

    If I need to specify alternate credentials, I can specify a remote computer name and an account that has local admin rights on the remote box. The code would appear like the following:

    Keep in mind that WMI does not permit alternate credentials for a local connection. Attempts to use alternate credentials for a local connection results in the error appearing here:

    1. PS C:\> gwmi win32\_networkadapter -Credential (Get-Credential)
    2. cmdlet Get-Credential at command pipeline position 1
    3. Supply values for the following parameters:
    4. Credential
    5. gwmi : User credentials cannot be used for local connections
    6. At line:1 char:
    7. + gwmi win32\_networkadapter -Credential (Get-Credential)
    8. + CategoryInfo : InvalidOperation: (:) [Get-WmiObject], ManagementException
    9. ds.GetWmiObjectCommand

    This error, for local connections, is not a Windows PowerShell thing, WMI has always behaved in this manner, even going back to the VBScript days.

    In Windows 8 (and above), I can use Windows PowerShell to stop or to start a network adapter by using one of the CIM commands. Of course, the function wraps the WMI class, but it also makes things really easy. The _netadapter _functions appear here (gcm is an alias for the Get-Command cmdlet)

    1. PS C:\> gcm -Noun netadapter | select name, modulename
    2. Name ModuleName
    3. ---- ----------
    4. Disable-NetAdapter NetAdapter
    5. Enable-NetAdapter NetAdapter
    6. Get-NetAdapter NetAdapter
    7. Rename-NetAdapter NetAdapter
    8. Restart-NetAdapter NetAdapter
    9. Set-NetAdapter NetAdapter

    NOTE: To enable or to disable network adapters requires admin rights. Therefore you must start the Windows PowerShell console with an account that has rights to perform the task.

    The various network adapters on my laptop appear in the figure that follows.

    1. Get-NetAdapter| ? status-ne up| Disable-NetAdapter

    The problem with the previous command is that it prompts. This is not much fun when there are multiple network adapters to disable. The prompt appears here.

    image027.png

    To suppress the prompt, I need to supply $false to the -confirm parameter. This appears here.

    A quick check in control panel shows the disconnected adapters are now disabled. This appears here.

    If I want to enable a specific network adapter, I use the Enable-Network adapter. I can specify by name as appears here.

    1. Enable-NetAdapter -Name ethernet -Confirm:$false

    If I do not want to type the adapter name, I can use the Get-NetAdapter cmdlet to retrieve a specific network adapter and then enable it. This appears here.

    1. Get-NetAdapter -Name vethernet\*| ? status -eq disabled| Enable-NetAdapter -Confirm:$false
    1. PS C:\> Get-NetAdapter -Name vethernet\* | Disable-NetAdapter
    2. Confirm
    3. Are you sure you want to perform this action?
    4. Disable-NetAdapter 'vEthernet (InternalSwitch)'
    5. [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help
    6. (default is "Y"):y
    7. Confirm
    8. Are you sure you want to perform this action?
    9. Disable-NetAdapter 'vEthernet (ExternalSwitch)'
    10. [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help