How to run netsh command through a batch file

ArcherARcher picture ArcherARcher · Aug 22, 2015 · Viewed 12.5k times · Source

I want to run the following commands through a batch file.

netsh
wlan
connect name=NETWORK-NAME

The issue is that 'netsh' and 'wlan' are unable to be done in the same line using conventional delimiters (&, &&). If I run this string as a batch file:

echo 1 & netsh & echo 2 & wlan & echo 3 & connect name=NETWORK-NAME

It outputs 1, activates netsh and stops there without executing the rest of the batch file.

Answer

Hardoman picture Hardoman · Nov 27, 2015

netsh is a program that can run either in interactive mode when you enter the program and you enter command modifiers and switch contexts one by one per line or just run the required operation in the required contect immediatelly. So you just need to make a single string:

@echo off
netsh wlan connect name=profile_name ssid=SSID_name interface="Wirelss network adapter"

Note that ssid and interface are optional. You need to specify interface if you have several wirelss network adapters in your system - it corresponds to the adapter name. And you need to specify ssid if there are multiple SSIDs in your saved wireless network profile.