Use PowerShell to Enable Wi-Fi

Doctor Scripto

Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to enable Wi-Fi if Ethernet is not connected.

Hey, Scripting Guy! Question Hey, Scripting Guy! Most of the time when I use my laptop running Windows 8.1, I am plugged in to the Ethernet network at work. Occasionally, I unplug the laptop and take it to meetings. I keep the Wi-Fi disabled when I am plugged in to the Ethernet because if I have it enabled, it seems that at random times, my laptop connects to Wi-Fi.

I only notice this when Outlook becomes really slow or I am trying to download something that ends up taking forever. I also think this is a security issue, because I should know what network I am connected to. But it seems to take me forever to make the change between enabling and disabling various network adapters. Can I script this?

—SC

Hey, Scripting Guy! Answer Hello SC,

Microsoft Scripting Guy, Ed Wilson, is here. This past weekend was pretty cool. The weather was wonderful and I was caught up on my book project, so the Scripting Wife and I headed out for a weekend excursion. Because we really did not have a set agenda, and because we had no specific timetable to adhere to, we sort of winged it. Armed with a couple of Windows Phones, a couple of Surfaces, and a couple of Zunes, we were ready for anything.

We ended up in a small town outside of Charlotte, and I found a highly recommended tea room on Yelp. So we detoured to go there. The tea selection was OK, but not great, so I opted for a triple shot espresso instead. Teresa, of course, had hot chocolate. Anyway, I like hand-crafted coffee if I don’t have to make it, and if there is not any good tea around. But like most things, if it becomes a regular thing, I want to automate it.

Your question gives me a perfect start for Hardware Week. One of the issues with my laptop is that it seems to keep growing new network adapters. Every time I update drivers or software, it seems that a new network adapter pops up. I can navigate to the network adapter folder. It is shown here:

Image of folders

But I can also use the Get-NetAdapter cmdlet to display the network adapters and their status. This is shown here:

Image of command output

I can use Get-NetAdapter and a wildcard configuration to find network adapters, or I can use the Name or InterfaceDescription properties. Each computer will have its own collection of adapters and names for those devices. Therefore, each script will need to be matched to the workstation that will run the script. It is possible to simplify this process by naming each adapter the same—but as I said, over time, my laptop seems to grow new network adapters, and these tend to take on default names.

For me, the best approach was to disable everything if I am not connected via the Ethernet cable, and then turn on the adapters I need. Because the command to disable the network adapters tends to be slow (or at least, take varying amounts of time), I decided to call the command as a job and wait for the job to complete. Because the cmdlet prompts by default, I set –Confirm to $False. This command is shown here:

If ( (Get-NetAdapter -Name 'Ethernet').Status -ne 'Connected' )

    {

      Get-NetAdapter | Disable-NetAdapter -Confirm:$false -AsJob | Wait-Job 

Now I need to enable the network adapters I want to use. Here is the command I use:

Enable-NetAdapter -Name 'Wi-Fi'-Confirm:$false

Enable-NetAdapter -Name 'Ethernet 3' -Confirm:$false

The complete script is shonw here:

If ( (Get-NetAdapter -Name 'Ethernet').Status -ne 'Connected' )

    {

      Get-NetAdapter | Disable-NetAdapter -Confirm:$false -AsJob | Wait-Job

      Enable-NetAdapter -Name 'Wi-Fi'-Confirm:$false

      Enable-NetAdapter -Name 'Ethernet 3' -Confirm:$false

      }

    Note  You need to run this script with admin rights. It also uses Windows 8.1, and therefore, it will not work on Windows 7.
    I talk about other approaches for performing this task in the Hey, Scripting Guy! Blog post called Enabling and Disabling Network Adapters.

SC, that is all there is to using Windows PowerShell to enable Wi-Fi if Ethernet is not connected. Hardware Week will continue tomorrow when I will talk about disabling Wi-Fi if Ethernet is connected.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

0 comments

Discussion is closed.

Feedback usabilla icon