In one of my previous posts I talked about how to disable/enable LAN connections on a Windows 2008 Core server using command line. In this post, I am going to present you a much enhanced version of it using powershell. This powershell script helps you to disable or enable any network connection in either local computer or remote computer easily. You can use this script to perform this operation on multiple computers — will give usage example at the end of this post.
In this script I am using Win32_NetworkAdapter WMI class which has methods for enabling or disabling a given network connection. By default, the script looks for network connections with the name Local Area Connection to perform disable or enable operations. If you want to perform these actions against different network connections(like HP Team#1) you can specify that using -ConnectionName parameter. The -Disable or -Enable parameters are for specifying the kind of operation you want to perform. The script will fail if you don’t find either of them. Similarly, you can use -ComputerName parameter if you want to perform this operation against remote computer. You can also provide the list of computers to this argument. I want you be more conscious while using this script against remote computer. The reason behind it is, if you are attempting to disable the primary network connection in remote computer, the script may not return the success/failure status. The reason is simple, it lost the connection to remote computer because you just disabled it. You may find this script handy when you want to disable non-primary connections on remote computers. Since the primary connection is ON in this case, this can return you the status. I didn’t perform any testing against remote computers explicitly so please do testing in your test lab before trying in production.
Code : Set-NetworkConnection
function Set-NetworkConnection { [cmdletbinding()] param ( [parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)] [string[]]$ComputerName = $env:computername, [switch]$Enable, [switch]$Disable, [String]$ConnectionName = "Local Area Connection" ) begin { if(-not($enable -or $disable)) { write-Error " You should select either of -Enable or -Disable switches. Exiting" exit } } process { foreach ($Computer in $ComputerName) { $Computer = $Computer.ToUpper() if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) { $Networks = Get-WmiObject Win32_NetworkAdapter -ComputerName $Computer | ? {$_.NetConnectionID -match $ConnectionName} foreach ($Network in $Networks) { if($Enable) { $retval = $Network.Enable() if($retval.ReturnValue -eq 0) { Write-host "Successfully enabled `"$($network.NetConnectionID)`" on $Computer" } else { write-host "Failed to enable `"$($network.NetConnectionID)`" on $Computer" } } elseif($Disable) { $retval = $Network.Disable() if($retval.returnValue -eq 0) { Write-host "Successfully disabled `"$($network.NetConnectionID)`" on $Computer" } else { write-host "Failed to disable `"$($network.NetConnectionID)`" on $Computer" } } } } } } end {} }
Usage Examples:
.\Set-NetworkConnection -Enable #to enable local network connection
.\Set-NetworkConnection -Disable #to disable local network connection
.\Set-NetworkConnection -Disable -ConnectionName “My network2” #to disable the local network connection that has the name “My Network2”
.\Set-NetworkConnection -ComputerName MyPC2 -Disable -ConnectionName “My network2” #to disable the network connection that has the name “My Network2” on MyPC2
Get-Content C:\mypcs.txt | Set-NetworkConnection -Disable -ConnectionName “My Network2” #To disable the network connection name “My network2” on list of PCs in C:\mypcs.txt file
Hope this helps… Happy learning…