0

I know that this topic has been covered in a lot of sites, but I couldn't really find the answer that I wanted.

I've been working on a batch file that would basically get the name of the network I'm connected to, and send a pop up message of the same. I was able to get the network profile with findstr but I couldn't find a way to filter it further so that it just prints out the Network name. Here's what I have so far:

netsh wlan show interfaces | findstr Profile

This gives out the output below:

Profile                : Alohomora-5G

Is there a way to make it just Alohomora-5G?

Jim Halpert
  • 37
  • 1
  • 5
  • 3
    Take a look at [`for /F`](https://ss64.com/nt/for_f.html)… – aschipfl Jul 03 '21 at 16:33
  • I couldn't make sense out of it cause I'm a bit new to batch files. Any idea if this is possible with powershell? – Jim Halpert Jul 03 '21 at 16:46
  • 2
    Example: `for /F "tokens=1* delims=: " %%I in ('%SystemRoot%\System32\netsh.exe wlan show interfaces ^| %SystemRoot%\System32\findstr.exe /L /C:Profile') do echo %%J` The last command `echo %%J` can be replaced by any other command or a block with multiple commands using `%%J` with `(` and `)` for beginning/end of the command block. The example command line is for usage in a batch file. Run the command line with just `%I` and `%J` instead of `%%I` and `%%J` directly in a command prompt window to get output just `Alohomora-5G`. – Mofi Jul 03 '21 at 17:37
  • 1
    Here you can find some examples: [How to set commands output as a variable in a batch file](https://stackoverflow.com/q/6359820) – aschipfl Jul 03 '21 at 20:48
  • 2
    In my experience you should use any method you can other than relying upon parsing the often poor fixed width formatted language dependent text output from the `netsh.exe` utility. Not only could the language mean that the English string `Profile` will not be returned, but the output profile name can, and often does, contain hidden space characters. In addition to that, there is no reason to assume that only one interface will be connected, and therefore NetSh may return more than one profile. – Compo Jul 04 '21 at 19:00

0 Answers0