• 1
name Punditsdkoslkdosdkoskdo

Getting SSID in Windows 10 CMD script

I need to pass the SSID name to my simple script with the main line as follows

netsh wlan set profileparameter name="SSID" cost=variable

So I know I can get the SSID from executing

Netsh WLAN show profiles

and looking at the line "Current user profile" find needed value. But without grep and sed how can I use the value from command output?

P.S. The idea is to use native Windows tools, not CigWin or UNIX services i.e. not to install any additional software if it is possible.


Use a for /f loop to get the output of a command. You could use proper tokens and delims to extract the part of the line you want, but the syntax is a bit weird to use quotes as delimter:

for /f tokens^=2delims^=^" %%a in ('Netsh WLAN show profile "profilename"^|find "SSID-Name"') do set "SSIDa=%%a"
echo a: %SSIDa%

Another possibilty is to use the colon as delimiter. But then you have to remove the extra space and the quotes:

for /f "tokens=2 delims=:" %%a in ('Netsh WLAN show profile "profilename"^|find "SSID-Name"') do set "SSIDb=%%~a"
echo b: %SSIDb:~2,-1%

You can also use a second for to get the last token of the line (which happens to be the name you want):

for /f "delims=" %%a in ('Netsh WLAN show profile "profilename"^|find "SSID-Name"') do for %%b in (%%a) do set "SSIDc=%%~b"
echo c: %SSIDc%

A fourth possibilty is to use tokens=3 (with standard delimiters), but that's a bad (insecure) choice because the output (number of tokens) might change with localization (on the other hand, also your search string SSID-Name is localization dependent).

  • 0
Reply Report