Disabling/Enabling Network Connection by Name

 

I was playing around with Windows PowerShell today and wanted to demonstrate a way to disable a network connection by the name or any other property for that matter. This entry will be limited to filtering on the name. First the network connections I care about I have below on my local machine.

image

To get this listing we need to use the WMI class called Win32_NetworkAdapter. Without going into too much detail, that class represents a network adapter of a computer. The link gives more detail if you want to show IPv6 data. This is currently limited to IPv4 information.

Anyhow, to get a listing, I run this command:

 Get-WmiObject -Class Win32_NetworkAdapter `
    -Filter "NetConnectionID IS NOT NULL" | 
   Format-Table -Property NetConnectionID, NetEnabled, InterfaceIndex `
    –AutoSize 

After running that command I will get the following output for my box.

NetConnectionID NetEnabled InterfaceIndex

--------------- ---------- --------------

Local Area Connection False 10

Wireless Network Connection 6 True 25

Wireless Network Connection 7 False 26

Your output probably differs. We are closer to achieving our goal. That WMI class gives us a few method to play with:

 Get-WmiObject -Class Win32_NetworkAdapter | 
    Get-Member -MemberType Method | 
        Format-Table -Property Name

TypeName: System.Management.ManagementObject#root\cimv2\Win32_NetworkAdapter

Name

----

Disable

Enable

Reset

SetPowerState

The WMI Class provides us with 4 methods that are usable if we run this script elevated. Remember, the act of modifying a network adapter requires elevated privileges.

To ENABLE a network adapter

 $networkAdapterName = "Wireless Network Connection 7"
Get-WmiObject -Class Win32_NetworkAdapter `
    -Filter "NetConnectionID = '$($networkAdapterName)'" | 
        ForEach-Object { $_.Enable() }

To DISABLE a network adapter

 $networkAdapterName = "Wireless Network Connection 7"
Get-WmiObject -Class Win32_NetworkAdapter `
    -Filter "NetConnectionID = '$($networkAdapterName)'" | 
        ForEach-Object { $_.Disable() }