So, I'm running the command netsh wlan show profiles
and I want to filter out all of the output except for the strings that include the SSID names into separate variables. The output looks similar to this:
All User Profile : String1
All User Profile : String2
All User Profile : String3
All User Profile : String4
All User Profile : String5
All User Profile : String6
All User Profile : String7
All User Profile : String8
and so on.
How would I go about getting each string by itself without the All User Profile :
behind it, and then setting it into a variable, with each string having its own seperate variable? I would like to keep it in CMD and Powershell. I know there's the CMD for
command, but the best I could come up with is
for /f "delims=" %%a in ('
netsh wlan show profiles
^| findstr " All User Profile : "
') do set "code=%%a"
which would only set one, and it also wouldn't filter out the All User Profile :
part of the command, it would take the whole line which is not what I want.
I found this page, but I don't think that will work.
EDIT: So I've made some progress, but I don't like it because it's sloppy and uses temporary files.
netsh wlan show profiles | findstr /v "Wi-Fi:" | findstr /v "profiles" | findstr /v "^-" | findstr /v "None" > test.txt
powershell -Command "(gc test.txt) -replace ' All User Profile : ', '' | sc test2.txt"
Also, doing this if I were to set the contents of the text file just made into a variable, it would only be the first line of the file, and would only make one variable, which are both problems.
EDIT 2: Made everything wayyy more specific.
EDIT 3: Ok, I'm so close now, sorry for not being specific enough earlier. This is what I have right now:
for /f "delims=" %%a in ('
powershell -command "netsh wlan show profiles | Select-String '^ All User Profile : (.*)' | ForEach-Object {$_.Matches[0].Groups[1].Value}"
') do set "code=%%a"
But the problem is that it sets each string as code, so they just overwrite each other. I'm thinking something like this but I don't know the syntax well enough.
for /f "delims=" %%a in ('
powershell -command "netsh wlan show profiles | Select-String '^ All User Profile : (.*)' | ForEach-Object {$_.Matches[0].Groups[1].Value}"
') do (
set "code1=%%a"
set "code2=%%a"
set "code3=%%a"
set "code4=%%a"
set "code5=%%a"
set "code6=%%a"
set "code7=%%a"
set "code8=%%a"
)
Here's the continuation of this
I did get it, here's the final code:
$array = netsh wlan show profiles |
ForEach-Object {
if ($_ -match "\s*All User Profile\s*:\s*(.*)") { $($matches[1]) }
}
foreach ($wn in $array) {
netsh WLAN show profile name=$wn
}