Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to disable Wi-Fi if Ethernet is connected.
Hey, Scripting Guy! When I have an Ethernet cable plugged in to my laptop and I am connected to a network, I want to ensure that everything else is disabled. How can I do that by using Windows PowerShell?
—DH
Hello DH,
Microsoft Scripting Guy, Ed Wilson, is here. This morning is a bit cool, and definitely moist outside. It is not raining, but it seems like it could easily do so. It sort of reminds me of winter mornings in Florida, when it was cool and damp. I have not seen any seagulls, but I would not be surprised if a few appeared. The big thing is to be prepared, because the weather can change suddenly and dramatically.
DH, the thing I like to do when setting up my laptop, or when setting up servers, is to provide meaningful names for things. If I rely on Windows naming conventions, I end up not aware of what a lot of stuff actually is. But by naming them myself, I can confirm that I know what each device really does.
This is the way I found out what was going on with my laptop after I added the Hyper-V role because it changed, added, and modified the way the network adapters did things. So, the best way to know what is going on is to make sure that you name network adapters so you know what they are. Here is a screenshot of my network adapters:
I have named my Ethernet, my virtual Ethernet adapters, the Wi-Fi, and the virtual Wi-Fi. I also renamed my internal switch. I did not bother naming the other stuff. This enables me to easily separate the network adapters I am concerned with.
To write the script, the first thing I need to do is to find if the Ethernet wired network connection is actually connected. If it is connected, I want everything except my virtual Ethernet adapter disabled. Here is the code that I use to determine if I am connected via Ethernet:
If ( (Get-NetAdapter -Name 'Ethernet').MediaConnectionState -eq 'Connected' )
Now I need to find everything that is not “Ethernet.” This is actually easy to do because I name my network adapters “Ethernet” or something like that. So in this line of code, I simply look for all network adapters not named something like Ethernet:
(Get-NetAdapter).where({$psitem.name -notmatch 'ethernet'}) |
I now pipeline the resulting network adapters to the Disable-NetAdapter cmdlet, and I specify that I do not want confirmation:
Disable-NetAdapter -Confirm:$false
When I run the script, I can see the results in the Network Connections pane. I check there because there is no output from the script as it is written. Here are the resulting changes:
For those who were paying close attention to the images, there was an “Ethernet 3” adapter that appeared in the first image, but it is not present in the second. This is where naming stuff makes it easier to keep track of things. The “Ethernet 3” adapter seems to appear when wireless is enabled, and it disappears when it is disabled.
This is why I do not need to worry about it in my script. Based on yesterday's script (see Use PowerShell to Enable Wi-Fi), I can say when Ethernet 3 is not enabled, wireless on my laptop does not appear to work. Based on that, I know it is important, but I really do not know where it came from, where it goes, or why Windows seems to feel it needs it. Oh well, the script works, and it does what I want it to do.
DH, that is all there is to using Windows PowerShell to disable network adapters. Join me tomorrow when Hardware Week continues, and I will talk about more cool stuff. Now, I think I will go outside and look for seagulls.
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