Enable/Disable Network Connection


Whilst waiting for lunch, i stumbled across this post on Channel9, https://channel9.msdn.com/ShowPost.aspx?PostID=158556. It is c# code for enabling and disabling network adapters. I thought you must be able to do this with PowerShell easier.

In Vista there is a win32_NetworkAdapter wmi provider – whoop. Problem solved.

PS C:\> Get-wmiobject win32_NetworkAdapter | format-table 

will get you a list of adapters installed on your machine. My wireless adapter was deviceId 5, so this line gets me the device

PS C:\> Get-WmiObject win32_networkadapter | where {$_.DeviceId -eq 5}


ServiceName      : NETw2v32
MACAddress       : 00:13:CE:8A:4B:C5
AdapterType      : Ethernet 802.3
DeviceID         : 5
Name             : Intel(R) PRO/Wireless 2915ABG Network Connection
NetworkAddresses :
Speed            : 11000000

Saving this to a variable to work with is easy

$wireless = Get-WmiObject win32_networkadapter | where {$_.DeviceId -eq 5}

Now i can interact with my wireless adapter and do such things as

$wireless.Disable()
$wireless.Enable()

Cool – add the variable to your profile and everytime you open up powershell you can interact with your network adapter, no C# console. No VBS to run. Nice clean and simple.


Comments (3)

  1. VC_RYK says:

    Hi Devid

    i want to disable ethernet and internet on a work station through VC++.So can you help me out urgenly.

    some code would be appreciable

    Thansk in advance

    RYK

  2. josh says:

    Here is what I get when I try to run $wireless=Disable()

    I am running powershell in windows XP

    Method invocation failed because [System.Management.ManagementObject#rootcimv2WiFi_NetworkAdapter] doesn’t contain a

    method named ‘Disable’.

    At line:1 char:18

    + $wireless.Disable( <<<< )

  3. daiken says:

    I’ve just looked on my XP machine and it appears that Disable is not supported on XP. It must be new to Windows Vista.