Display all user profiles in variable - cmd

I need the output of all User profiles only with their names without the first column "All User Profile"
Example:
All User Profile : WifiName
Output:
WifiName
I am using:
for /f %%a in ('netsh wlan show profile | find /i "all user profile"') do set "%%~a"
echo %a% > C:\test.txt

#echo off
for /f "tokens=2 skip=1 delims=:" %%a in ('netsh wlan show profile') do echo %%a >> .\test.txt

Related

How do you set a specific part of the output of a CMD batch command as a variable? or alternatively, just echo it?

I'm writing a simple little batch file that gets the password of a saved Wi-Fi network, but I want to grab the Key Content, then paste it on its own. Here's the current code:
#echo off
set /p name=Enter Wi-Fi Name:
cls
echo %name%
netsh wlan show profile name="%name%" key=clear
cmd /k
This gives me a long list of data, but the line I'm looking for is the "Key Content" line. What I essentially want to do is grab the "Key Content" line, clear all the lines, then echo the "Key Content" line. Is this possible without any plugins on Windows 11?
I'm new to the site and coding as a whole, by the way, so what may seem like something completely obvious to you is something I have a 95 percent chance not to know. Thank you!
Here's a batch-file snippet, based upon you having received and properly validated the end users input:
#For /F "Tokens=1,* Delims=:" %%H In ('
%SystemRoot%\System32\netsh.exe WLAN Show Profiles Name^="%name%" Key^=Clear
2^>NUL ^| %SystemRoot%\System32\findstr.exe /RIC:"^[ ][ ]*Key Content[ ][ ]*: "
') Do #(Set "}=%%I" & SetLocal EnableDelayedExpansion
For %%J In ("!}:~1!") Do #EndLocal & Echo(%%J)
You should always perform robust validation of any end users input, especially when using the Set /P command, which can accept nothing or absolutely anything, (including malicious content). If the target systems are Windows 8 / Server 2012 or above, then I wouldn't waste time asking the end user to self-determine the wireless profile name, and then type it correctly at a prompt. I'd just output all of the profile names, along side their returned keys.
The following examples are untested, and assume that your 'passwords' do not include double-quote characters. They should provide the output in a CSV-like format, ("ProfileName","Password"):
#For /F Tokens^=6^ Delims^=^" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe
/NameSpace:\\Root\StandardCimv2 Path MSFT_NetConnectionProfile Get Name
/Format:MOF 2^>NUL') Do #For /F "Tokens=1,* Delims=:" %%H In ('
%SystemRoot%\System32\netsh.exe WLAN Show Profiles Name^="%%G" Key^=Clear
2^>NUL ^| %SystemRoot%\System32\findstr.exe /RIC:"^[ ][ ]*Key Content[ ][ ]*: "
') Do #(Set "}=%%I" & SetLocal EnableDelayedExpansion
For %%J In ("!}:~1!") Do #EndLocal & Echo("%%G",%%J)
#Pause
As you appear to have used the cmd tag, despite your question being about a batch-file, you could probably do similarly, directly in the Command Prompt, (cmd.exe) too:
For /F Tokens^=6^ Delims^=^" %G In ('%SystemRoot%\System32\wbem\WMIC.exe /NameSpace:\\Root\StandardCimv2 Path MSFT_NetConnectionProfile Get Name /Format:MOF 2^>NUL') Do #For /F "Tokens=1,* Delims=:" %H In ('"%SystemRoot%\System32\netsh.exe WLAN Show Profiles Name="%G" Key=Clear 2>NUL | %SystemRoot%\System32\findstr.exe /RIC:"^[ ][ ]*Key Content[ ][ ]*: ""') Do #Set "}=%I" & For /F Delims^=^ EOL^= %J In ('%SystemRoot%\System32\cmd.exe /V /D /C "Echo "%G","!}:~1!""') Do #(Set /P ="%J") 0<NUL & Echo(

Saving command output to variable is not working

I'd like to create simple script to show wlan password on Windows10.
This will be well solution for users that's not familiar with cmd commands.
On Windows 7 it could be done using GUI, but not on newer OSes.
I stuck on line
for /f "tokens=*" %%j in ('netsh wlan show profile %ssid% key=clear ^| findstr "Key Content"') do set wlan_password=%%j
The variable wlan_password is always null. Even if i change set instruction to e.g. echo it shows that syntax is incorrect. I can't troubleshoot that.
Why the line above doesn't work, but the line:
for /f "tokens=*" %%i in ('netsh wlan show interfaces ^| findstr "Profile"') do set wlan_output=%%i
works well?
#echo off
set wlan_output=
set connected_ssid=
set ssid=
set wlan_password=
for /f "tokens=*" %%i in ('netsh wlan show interfaces ^| findstr "Profile"') do set wlan_output=%%i
for /f "tokens=2 delims=:" %%a in ("%wlan_output%") do set connected_ssid=%%a
call :TRIM %connected_ssid% connected_ssid
set ssid=%1
if "%ssid%"=="" set /p "ssid=Podaj nazwe sieci [%connected_ssid%]: " || set "ssid=%connected_ssid%"
if not "%ssid%"=="" (
for /f "tokens=*" %%j in ('netsh wlan show profile %ssid% key=clear ^| findstr "Key Content"') do set wlan_password=%%j
echo "Haslo do sieci %ssid%: %wlan_password%"
exit /b
)
else (
echo "Nie podano nazwy sieci. Nie mozna odczytac hasla"
exit /b
)
pause
exit /b
:TRIM
SET %2=%1
GOTO :EOF
Ugh - sorry for not spotting the real problem sooner: you have also to escape the = within the for command:
for /f "tokens=*" %%j in ('netsh wlan show profile %ssid% key^=clear ^| findstr /c:"Key Content"') do set wlan_password=%%j
set wlan_password
Note: use findstr /c:"Key Content" or find "Key Content", because findstr "Key Content" returns each line that contains Key OR Content (or both). (Not that it would make any difference in this special case, but without /c: it will bite you sooner or later)
To get the key only:
for /f "tokens=1,* delims=:" %%j in ('netsh wlan show profile %ssid% key^=clear ^| find "Key Content"') do set "wlan_password=%%k"
set "wlan_password=%wlan_password:~1%"
echo ---%wlan_password%---

Get stored WiFi networks with a cmd oneliner

I would like to run this as a 1 line command (not from a batch), but however I try, I cannot succeed:
(for /f "tokens=2 delims=:" %%a in ('netsh wlan show profiles ^| findstr "Profile"') do (
set str=%%a
set str=!str:~1!
echo !str!
)) >> wifi_networks.txt
This is what I tried:
(for /f "tokens=2 delims=:" %%a in ('netsh wlan show profiles ^| findstr "Profile"') do (set str=%%a & set str=!str:~1! & echo !str!)) >> wifi_networks.txt
Any help please?
When using a batch file, you double up the %'s, as you aren't they should be returned to single again.
Try this, (untested):
(For /F "Tokens=2Delims=:" %A In ('NetSh WLAN Show Profiles^|Find "Profile"') Do #For /F "Tokens=*Delims= " %B In ("%A") Do #Echo %B)>>"wifi_networks.txt"
If wifi_networks.txt doesn't already exist you can change >> to >.

Extracting Network Profiles and displaying only Network Profile description in txt file

I am very new to batch scripting and have to use console to interrogate Registry for network profile description and output only the description data to a txt file.
I am using a for /f loop to do this.
I first reg query the whole key so it lists every sub key for network profiles and stores this in a text document. I then for /f this text file to extract out only the subkey name using tokens to store this as a variable.
I then use the variable to reg query the individual keys for the Description name and output this to another text file which should display only the Network profile description. Below is my batch script.
Echo Required to skip line for processing >>%~dp0\1SSID.txt
REG QUERY "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles" /s /v Description >>%~dp0\1SSID.txt
setlocal enableDelayedExpansion
rem get each key from 1SSID.txt
for /f "usebackq skip=1 tokens=1,2" %%i in ("%~dp0\1SSID.txt") do (
echo %%i %%j>>%~dp0\2Processingstage.txt
rem skip the first line and grab tokens 3 from the second line to show description and desription name
for /f "usebackq skip=1 tokens=3" %%k in (`reg query "%%I %%j" /v Description`) do set "Description=%%l
echo Network Description - %%l >>%~dp0\3SSIDoutput.txt
)
)
The first think I notice is the skip=1 doesn't work and look at every line.
As this doesn't work it doesn't extract the correct data to place into the reg query. I have tried with different tokens, without skip, with skip, with delims (which it didn't recognise). Ive been working on this for hours and simply cant get it to work. This is probably simple but I cant find a way around this.
Here is an quick example which should output a file providing each GUID and Description alongside your running script.
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
(Set k=HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles)
For /F "Delims==" %%A In ('Set GUID[ 2^>Nul') Do Set "%%A="
Set "i=101"
For /F "EOL=E Tokens=1,2*" %%A In ('Reg Query "%k%" /S /V Description') Do (
If "%%~nB" NEq "%%~B" (Call Set "GUID[%%i:*1=%%]=%%~nB") Else (
Call Call Set GUID[%%i:*1=%%]="%%%%GUID[%%i:*1=%%]%%%%","%%C"
Set/A i+=1))
If %i% NEq 101 (>"%~dp0NetProfs.log" 2>Nul Set GUID[)
EndLocal
Exit/B
You will probably need to right-click and run as Administrator due to resrictions on those keys.
From this thread : Wi-Fi SSID detail
I don't know if this code works over english machines or not, because, i just tried it until now, on my french machine.
so, just give a try and tell me the results :
#echo off & setlocal enabledelayedexpansion & color 0A
Title %~n0 to get SSID With details
::::::::::::::::::::::::::::::::::::::::::::
:: Automatically check & get admin rights ::
::::::::::::::::::::::::::::::::::::::::::::
Set TmpLogFile=%tmp%\TmpLog.txt
If Exist %TmpLogFile% Del %TmpLogFile%
REM --> Check for permissions
Reg query "HKU\S-1-5-19\Environment" >%TmpLogFile% 2>&1
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
Echo.
ECHO ****************************************
ECHO ^| Running Admin shell... Please wait...^|
ECHO ****************************************
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
set params = %*:"=""
echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B
:gotAdmin
::::::::::::::::::::::::::::
::START
::::::::::::::::::::::::::::
Set "TmpLog=%~dp0%~n0_Tmp.txt"
Set "Log=%~dp0%~n0.txt"
If Exist "%TmpLog%" Del "%TmpLog%"
If Exist "%Log%" Del "%Log%"
rem Populate the array
Set i=0
for /f "skip=1 tokens=2 delims=:" %%a in ('netsh wlan show profiles ^|find /i "Profil"') do (
set /A i+=1
set "list[!i!]=%%a"
)
set SSID=%i%
rem Display array elements for SSID List
cls
for /L %%i in (1,1,%SSID%) do (
echo(
echo SSID number %%i: "!list[%%i]:~1!!"
echo(
)
pause
rem Display array elements for SSID List with details
cls
for /L %%i in (1,1,%SSID%) do (
echo(
echo SSID number %%i: "!list[%%i]:~1!!"
echo(
netsh wlan show profiles "!list[%%i]:~1!!" key=clear
netsh wlan show profiles "!list[%%i]:~1!!" key=clear >> "%TmpLog%"
)
Cmd /U /C Type "%TmpLog%" > "%Log%"
If Exist "%TmpLog%" Del "%TmpLog%"
Start "" "%Log%"
pause
exit /b

How do I ping the default gateway without specifying the IP address?

I'm trying to write a batch file that tests network connectivity by pinging the default gateway of the given network.
However, as I want this to be an automated process which then logs the results to a text file, I'd like to be able to ping the gateway on various networks, without having to change/enter the ip address.
Is there a generic term or command to ping the default gateway for the network you're currently connected to?
(I already have the commands for output options etc.)
So far, I have this....
#echo off
goto :NETWORK1
:NETWORK1
ipconfig
echo .
Set /P gateway=PLEASE ENTER GATEWAY IP ADDRESS (shown above):
if /I "%gateway%" EQU "exit" goto :EXIT
if /I not "%gateway%" EQU "exit" goto :NETWORK2
:NETWORK2
echo CLOSING THIS WINDOW WILL ABORT THE CONNECTIVITY TEST.
echo . >> "C:\Network Test Results %date:/=.%.txt"
echo Time: >> "C:\Network Test Results %date:/=.%.txt"
time /t >> "C:\Network Test Results %date:/=.%.txt"
ping %gateway% -n 20 >> "C:\Network Test Results %date:/=.%.txt"
goto :NETWORK2
:EXIT
exit
But I don't want to have to specify the IP address, so that I can take away the need for user input.
smalll fix for Wernfried Domscheit
for /f "tokens=2 delims=:" %%g in ('netsh interface ip show address ^| findstr /c:"Default Gateway"') do ping %%g
pause
just adding /c: before "Default Gateway"
On a command line you can try this one:
for /f "tokens=2 delims=:" %g in ('netsh interface ip show address ^| findstr "Default Gateway"') do ping %g
Note, inside a batch-file you must double the %, i.e.
for /f "tokens=2 delims=:" %%g in ('netsh interface ip show address ^| findstr "Default Gateway"') do ping %%g
According to jimbobomcgee on serverfault who phrased the correct answer for the question How to Extract command-line output into a Variable?
for /f "usebackq tokens=1,2,3 delims=:" %A in (`ipconfig ^| Find "Default Gateway" ^| Findstr/N "." ^| Findstr/B "1:"`) do #if not defined MYVAR set MYVAR=%~C
#echo off
setlocal enableextensions disabledelayedexpansion
set "gateway="
for /f "tokens=1-5" %%a in ('route -4 print 0.*') do #if "%%e"=="" if "%%a"=="%%b" set "gateway=%%c"
if not defined gateway goto :eof
echo CLOSING THIS WINDOW WILL ABORT THE CONNECTIVITY TEST.
:loop
echo %date% %time%
>> "Network Test Results %date:/=.%.txt" (
echo(
echo(Time: %time%
ping -n 20 -4 %gateway%
)
goto :loop
Gateway determination from route information copied from here
edited for a more tolerant parse of the route command use
for /f "tokens=3" %%a in ('route -4 print 0.* ^| find "0."') do set "gateway=%%a"
After help from #MC ND, I have created the following code, which does exactly what I needed....
pings the default gateway with the results being output to a text file
shows a message telling the user not to close the command window
#echo off
setlocal enableextensions disabledelayedexpansion
for /f "tokens=3" %%a in ('route -4 print 0.*') do set "gateway=%%a"
:NETWORK
echo CLOSING THIS WINDOW WILL ABORT THE CONNECTIVITY TEST.
echo Time >> "C:\Network Test Results %date:/=.%.txt"
time /t >> "C:\Network Test Results %date:/=.%.txt"
echo . >> "C:\Network Test Results %date:/=.%.txt"
ping %gateway% -n 20 >> "C:\Network Test Results %date:/=.%.txt"
goto :NETWORK

Resources