Batch file to disable or enable network adapter

Tags:
Add new tag
Batch files
Local area networks
Network Adapters
Network Interface Cards
Network management
Dear all, Is there anyone that knows how to write a batch file to enable / disable local area connection just by 1 click on the batch file? Thank you so much...

Answer Wiki

Thanks. We'll let you know when a new response is added.

Use the netsh command in your batch file:

To disable the interface run:

netsh interface set interface "Local Area Connection" DISABLED

To enable the interface run:

netsh interface set interface "Local Area Connection" ENABLED

If the OS is Win XP the above method will probably not work.

In that case, here is another (a little more complicated) option that works on XP using VB Script. If the interface is enabled this script will disable it, and it will enable it when it is disabled.

Const ssfCONTROLS = 3 

sConnectionName = "Local Area Connection" 

sEnableVerb = "En&able" 
sDisableVerb = "Disa&ble" 

set shellApp = createobject("shell.application") 
set oControlPanel = shellApp.Namespace(ssfCONTROLS) 

set oNetConnections = nothing 
for each folderitem in oControlPanel.items 
	if folderitem.name = "Network Connections" then 
		set oNetConnections = folderitem.getfolder: exit for 
	end if 
next 
if oNetConnections is nothing then 
	msgbox "Couldn't find 'Network Connections' folder" 
	wscript.quit 
end if 
set oLanConnection = nothing 
for each folderitem in oNetConnections.items 
	if lcase(folderitem.name) = lcase(sConnectionName) then 
		set oLanConnection = folderitem: exit for 
	end if 
next 
if oLanConnection is nothing then 
	msgbox "Couldn't find '" & sConnectionName & "' item" 
	wscript.quit 
end if 

bEnabled = true 
set oEnableVerb = nothing 
set oDisableVerb = nothing 
for each verb in oLanConnection.verbs 
	if verb.name = sEnableVerb then 
		set oEnableVerb = verb 
		bEnabled = false 
	end if 
	if verb.name = sDisableVerb then 
		set oDisableVerb = verb 
	end if 
next 
if bEnabled then 
	oDisableVerb.DoIt()
else 
	oEnableVerb.DoIt 
end if 

wscript.sleep 500

Discuss This Question: 15  Replies

 
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 members answer or reply to this question.

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
  • Powerofnetworking
    But, I get this answer: " C:Users>netsh interface set interface "Local Area Connection" DISABLE D An interface with this name is not registered with the router. " Thanks for the help.
    45 pointsBadges:
    report
  • carlosdl
    You might want to provide more details. What is the OS ? (this method won't work on XP) Have you checked the interface names ? (ipconfig)
    68,650 pointsBadges:
    report
  • Matt Mather
    You need to actually specify your interface name, it could be called something else, see the output from my Windows machine: d:Documents and SettingsUserDesktop>netsh interface show interface Admin State State Type Interface Name ------------------------------------------------------------------------- Enabled Dedicated Network Connect Adapter Enabled Dedicated {32DAFD15-86C5-41C3-B398-EF8B4FA4 AAD4} Enabled Dedicated {64D62236-302F-4DFA-81AD-19E82BB1 DB41} Enabled Dedicated Wireless Network Connection Enabled Dedicated Local Area Connection Enabled Dedicated 1394 Connection Enabled Internal Internal Enabled Loopback Loopback You can see the interface name in the last column. You can also type simply netsh, then help to see all the commands available in the application.
    3,610 pointsBadges:
    report
  • Matt Mather
    My Windows machine is actually my work PC and it is running XP SP2. As Carlosdl mentioned if you are still having problems then please specify your exact OS.
    3,610 pointsBadges:
    report
  • carlosdl
    Mattmather, have you tested it on your xp machine ? I remember that NETSH can't modify the status of the network interfaces in XP as the OS sees them as "dedicated".
    68,650 pointsBadges:
    report
  • Matt Mather
    I have used it in the past but not tested on this machine as I am using it. A little google has confirmed an issue as mentioned in . An alternative suggestion could be to use wmic and there is a pretty darn good demo here:
    3,610 pointsBadges:
    report
  • Matt Mather
    This links should have been MS Support and the alternative at Using wmic
    3,610 pointsBadges:
    report
  • Powerofnetworking
    Are there any changes that I need to make? Because, when I run this command, it is fail to disable the LAN... Any further explanation?? Thank you...
    45 pointsBadges:
    report
  • carlosdl
    Powerofnetworking, what command are you referring to ? As you can see, at least three options have been mentioned in the answer and discussions.
    68,650 pointsBadges:
    report
  • Powerofnetworking
    From your latest answer...
    45 pointsBadges:
    report
  • carlosdl
    So you are using the VB script, right ? Are you getting errors ? if so, what is the complete error message ? Please, help us help you.
    68,650 pointsBadges:
    report
  • Powerofnetworking
    Do you mean that I need to download and install the VBScript first? After that, only run the batch file that u provided?? Thanks a lot...
    45 pointsBadges:
    report
  • carlosdl
    This is why I asked about the command you were referring to. And it seems that "your last answer" meant different things for you and for me. You have to understand that we don't know what you are doing, and we don't know the exact results you are getting, and we know nothing about the machine you are doing this on. If you don't provide more information, we won't be able to help you. In the 'answer' section I posted a vb script (which I presented as an alternative since the netsh option wouldn't work on windows xp, although we still don't know the operating system involved). If you want to use that option, you just have to create a text file with '.vbs' extension and put the provided script text in there. After saving the file you would only need to double click on it to execute the script.
    68,650 pointsBadges:
    report
  • RogersGuedes
    I have got the same error of "Powerofnetworking" (An interface with this name is not registered with the router).
    My solution was right click on my ".bat" and choose "Run as Administrator". Ha, my SO is Windows 7 x86 Ultimate.
    Thanks to you all!
    10 pointsBadges:
    report
  • caturpunya
    for vbs, work great in win xp. thanks,
    10 pointsBadges:
    report

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:

To follow this tag...

There was an error processing your information. Please try again later.

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

Thanks! We'll email you when relevant content is added and updated.

Following