Home > Back-end >  How to display the name of the network to which it is connected using cmd/bat
How to display the name of the network to which it is connected using cmd/bat

Time:04-01

I want to display the name of the network you are connected to in cmd, I tried to create a script

netsh wlan show interfaces | findstr /C:"SSID:"

But when I run the script it shows this result

SSID: networkname

I want a way to display only the network name without spaces and without displaying the word SSID and :

CodePudding user response:

I would offer an alternative using WMI:

Getting help from :

@For /F Tokens^=6^ Delims^=^" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe
 /NameSpace:\\Root\StandardCimv2 Path MSFT_NetConnectionProfile Where
 "IPv4Connectivity='4' And Name Is Not Null" Get Name /Format:MOF 2^>NUL'
) Do @Echo %%G

Getting help from :

<!-- ::Script Language="Batch"
@For /F %%G In ('%SystemRoot%\System32\cscript.exe //NoLogo "%~f0?.wsf"')Do @Echo(%%G
@Pause&Exit /B
--><Job><Script Language="VBScript">
        Set objWMI = GetObject("WINMGMTS:\\.\ROOT\StandardCimv2")
        WQL = "Select * From MSFT_NetConnectionProfile"  _
         " Where IPv4Connectivity='4' And Name Is Not Null"
        Set Instances = objWMI.ExecQuery(WQL)
        For Each Instance In Instances 
            Wscript.Echo Instance.Name
        Next</Script></Job>

Should you have wanted the SSID using your initial command you should be aware that the output from NetSh is in tabular format and could include or exclude space characters. I might therefore offer this type of methodology:

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "SSID=" & For /F "Tokens=1,* Delims=:" %%G In ('
 %SystemRoot%\System32\netsh.exe WLAN Show Interfaces 2^>NUL ^|
 %SystemRoot%\System32\findstr.exe /R /C:"^[ ][ ]*SSID[ ][ ]*:[ ][ ]*[^ ][^ ]*"'
) Do (Set "SSID=%%H" & SetLocal EnableDelayedExpansion
    For /F Delims^=^ EOL^= %%I In ("!SSID:~1!") Do EndLocal & Set "SSID=%%I")
If Defined SSID (Echo "%SSID%"
    Pause)
EndLocal
GoTo :EOF

Using that same methodology, you could also isolate the Profile name instead:

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "Profile=" & For /F "Tokens=1,* Delims=:" %%G In ('
 %SystemRoot%\System32\netsh.exe WLAN Show Interfaces 2^>NUL ^|
 %SystemRoot%\System32\findstr.exe /R
 /C:"^[ ][ ]*Profile[ ][ ]*:[ ][ ]*[^ ][^ ]*"') Do (Set "Profile=%%H"
    SetLocal EnableDelayedExpansion & For /F Delims^=^ EOL^= %%I In (
        "!Profile:~1!") Do EndLocal & Set "Profile=%%I")
If Defined Profile (Echo "%Profile%"
    Pause)
EndLocal
GoTo :EOF

You may notice, as my experience very often has, that your result includes a trailing space, which could potentially prove problematic. For that reason, I'd advise that you do not rely upon the tabular output from NetSh.

CodePudding user response:

Use a for /f:

for /f "usebackq tokens=2 delims= " %%N in (`netsh wlan show interfaces ^| findstr /C:"SSID:") do (
  set nw_name=%%N
  echo Found a network: !nw_name!
)

This for will split the result of the filtered netsh command (usebackq) on spaces (delims), will take the 2nd token (tokens), then you'll find it in loop variable %%N. Take care to put it in another variable, because %%N do not exist anymore once loop is finished.

See also for /? on command prompt for details.

CodePudding user response:

I am glad to see you already have an answer. Here is another way to do it that will run in a batch-file run by cmd.

FOR /F "delims=" %%A IN ('powershell -NoLogo -NoProfile -Command ^
    "(Get-NetConnectionProfile | Where-Object InterfaceAlias -eq 'Wi-Fi').Name"') DO (SET "nw_name=%%~A")
ECHO %nw_name%
  • Related