Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell 3.0 to stop network adapters on Windows 8.
Microsoft Scripting Guy, Ed Wilson, is here. The other day a customer asked me, “Is it a Microsoft requirement that all their consultants can’t be more than five miles from a coffee house?” Perhaps a little bit of background is in order. I was onsite with a large Microsoft premier customer, delivering a Windows PowerShell class. Their TAM, Robert, is one of my good friends at Microsoft, and I have worked with him for over a decade. Because I miss my teapot and my stash of teas, I developed the habit of stopping by the coffee house after lunch and getting a large cup of green tea—they actually have a pretty nice green tea with spearmint and lemon grass in it. They had no monkey-picked and no jasmine; but after all, it is a coffee house and not a tearoom. I was happy with what I could obtain. I looked around, and all of the Microsoft people (there were other consultants working on various projects as well) had similar looking large cups of coffee. And thus the question. Although both Microsoft and this particular coffee house chain are based in Seattle, as far as I know, there is no other relationship—and most certainly, there is no proximity-based requirement.
Stopping network adapters
Windows Vista introduced the ability to stop a network adapter by using the Win32_NetworkAdapter WMI class. In the following example, I use the Get-CimClass cmdlet from Windows PowerShell 3.0 to return cimclassmethods from the Win32_NetworkAdapter WMI class. I use the dotted notation and automatic foreach technique to pipe the resulting class methods to Where-Object (? is the alias), where I use the simplified Where-Object syntax from Windows PowerShell 3.0 to find only the implemented cimclassmethods. (For more information about these types of Windows PowerShell 3.0 syntax tricks, see My Five Favorite PowerShell 3.0 Tips and Tricks.) The Win32_NetworkAdapter class methods are shown here.
PS C:\> (Get-CimClass win32_networkadapter).cimclassmethods | ? qualifiers -match implemented
Name ReturnType Parameters Qualifiers
---- ---------- ---------- ----------
Enable UInt32 {} {Implemented, Map...
Disable UInt32 {} {Implemented, Map...
In Windows 8 and Windows Server 2012, I can use Windows PowerShell 3.0 to stop or to start a network adapter by using one of the CIM commands. Of course, the function wraps the WMI class, but it also makes things really easy. The netadapter functions are shown here (gcm is an alias for the Get-Command cmdlet.)
PS C:\> gcm -Noun netadapter | select name, modulename
Name ModuleName
---- ----------
Disable-NetAdapter NetAdapter
Enable-NetAdapter NetAdapter
Get-NetAdapter NetAdapter
Rename-NetAdapter NetAdapter
Restart-NetAdapter NetAdapter
Set-NetAdapter NetAdapter
Note To enable or disable network adapters requires admin rights. Therefore, you must start the Windows PowerShell console with an account that has rights to perform the task.
The various network adapters on my laptop are shown in the image that follows.
I do not like having enabled, disconnected network adapters. Instead, I prefer to only enable the network adapter that I am using. (There are a number of reasons for this, such as simplified routing tables, performance issues, and security concerns.) In the past, I wrote a script. Now I only need to use a Windows PowerShell command. If I only want to disable the non-connected network adapters, the command is easy. It is shown here.
Get-NetAdapter | ? status -ne up | Disable-NetAdapter
The problem with the previous command is that it prompts as shown in the following image. This is not much fun when there are multiple network adapters to disable.
To suppress the prompt, I need to supply $false to the –confirm parameter. This is shown here.
Get-NetAdapter | ? status -ne up | Disable-NetAdapter -Confirm:$false
A quick check in Control Panel shows the disconnected adapters are now disabled. This appears here.
If I want to enable a specific network adapter, I use the Enable-Network adapter. I can specify by name as shown here.
Enable-NetAdapter -Name ethernet -Confirm:$false
If I do not want to type the adapter name, I can use the Get-NetAdapter cmdlet to retrieve a specific network adapter and then enable it as shown here.
Get-NetAdapter -Name vethernet* | ? status -eq disabled | Enable-NetAdapter -Confirm:$false
It is also possible to use wildcard characters with the Get-NetAdapter to retrieve multiple adapters, and pipe them directly to the Disable-NetAdapter cmdlet. The following code permits the confirmation prompts so that I can selectively enable or disable the adapter as I wish.
PS C:\> Get-NetAdapter -Name vethernet* | Disable-NetAdapter
Confirm
Are you sure you want to perform this action?
Disable-NetAdapter 'vEthernet (InternalSwitch)'
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help
(default is "Y"):y
Confirm
Are you sure you want to perform this action?
Disable-NetAdapter 'vEthernet (ExternalSwitch)'
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help
(default is "Y"):n
That is about it for now. Join me tomorrow when I will talk about more cool Windows PowerShell stuff.
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