PowerShell for Windows Admins

Jun 2 2012   8:19AM GMT

Toggling between LAN and Wireless

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

One of the questions in the recent Scripting Games involved toggling between a wireless and LAN connection. Only one was to be up at any one time.

This can be solved using WMI but becomes hugely simpler in Windows 8/2012 as we get a bunch of cmdlets for working with network adapters.

Using WMI

Get-WmiObject -Class Win32_NetworkAdapter | Measure-Object

I get 15 objects returned on my system – but I’m only interested in 2 of them!

Using the new functionality

PS> Get-NetAdapter | ft -a Name, ifIndex, Status

Name             ifIndex Status
—-             ——- ——
Virtual Wireless      21 Disabled
Virtual LAN           12 Up

This leads to a simple piece of code

Get-NetAdapter |
foreach {
  $nic = $_
  switch ($_.Status){
   "Up"        {Disable-NetAdapter -InputObject $nic -Confirm:$false}
   "Disabled"  {Enable-NetAdapter -InputObject $nic -Confirm:$false}

  }
}

Get the adapter, test its status and toggle to the other.

These cmdlets are in the NetAdapter module which is created using the cmdlets over objects techniques to utilise WMI classes in a much friendlier way. Remoting capabilities are supplied by CIMsessions

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: