3

I have run the following command line,

netsh wlan show profile MyWiFi-name key=clear >key.txt

Output, key.txt

Profile MyWiFi-name on interface Wireless Network Connection: 
======================================================================= 

Applied: All User Profile    

Profile information 
------------------- 
    Version                : 1
    Type                   : Wireless LAN
    Name                   : MyWiFi-name
    Control options        : 
        Connection mode    : Connect automatically
        Network broadcast  : Connect only if this network is broadcasting
        AutoSwitch         : Do not switch to other networks

Connectivity settings 
--------------------- 
    Number of SSIDs        : 1
    SSID name              : "MyWiFi-name"
    Network type           : Infrastructure
    Radio type             : [ Any Radio Type ]
    Vendor extension          : Not present

Security settings 
----------------- 
    Authentication         : WPA-Personal
    Cipher                 : CCMP
    Security key           : Present
    Key Content            : password@1

My need is , I need only Key Content as a output in text file. Please help.

FOR EXAMPLE : expected output in key.txt file is,

password@1
DavidPostill
  • 153,128
  • 77
  • 353
  • 394
Philip
  • 676
  • 3
  • 11
  • 31
  • 2
    if you do `mycmd | find /i "key content"` then you can get just the one line you want and then your problem is smaller, as you have the line you want, you then just need to get the last 'word'. There is the 'for /f' command, it can tokenize, there are examples, it is a bit tricky, maybe somebody here can post a solution for you with it. I'd recommend installing a normal programming language(scripting or not), rather than batch which is about as functional as a blind paraplegic. Still it's very doable in batch. – barlop Feb 20 '18 at 12:13
  • 1
    @barlop +1000 for "as functional as a blind paraplegic." =P – Anaksunaman Feb 21 '18 at 06:17
  • @barlop Not tricky at all - it's trivial :) – DavidPostill Feb 21 '18 at 14:38
  • 1
    @DavidPostill easily doable, but compared to other languages, it's not trivial to do. Compare to ruby. `a=\`type abc.txt\`` `puts a.scan(/Key Content.*?$/)[0].split(": ")[1]` THAT, is trivial to do. – barlop Feb 21 '18 at 15:19
  • @barlop Not for me, but I speak batch not ruby ;) – DavidPostill Feb 21 '18 at 15:20
  • @DavidPostill Now consider that I have hardly used that ruby scan method, I didn't even know it was called scan I just looked it up in 2min to see it was scan. i'm not fluent in it at all. I am fluent in regexes (a skill worth learning).I have used the for /f statement many times, maybe once every 6 months for over a decade..but it's easy to forget details like escaping the pipe or to lose fluency on those for /f idiosyncracies. I have far more experience with batch than in ruby(which I haven't even known for 3 months).The ruby methods of scan and split were simple. Besides it was two lines. – barlop Feb 21 '18 at 15:24
  • @DavidPostill And it's not like I don't know batch files.. I wrote this one in 2015 https://superuser.com/questions/895168/how-to-measure-total-ram-usage-of-a-program-under-windows which was some tricky thing in batch..and involved for /f, and i've probably written answers explaining for /f to people..So, I know both.. But having not scanned a string in a while, I can quickly look up how to do it in any language, e.g. in ruby, and do it.Even if I haven't done anything like it in that language for a long while. And there are no weird idiosyncracies to remember there. Not so with for /f batch. – barlop Feb 21 '18 at 15:31

3 Answers3

1

I made before a batch script to find and display all of SSID registered on a PC with their passwords and save them in a text file.

Wifi Passwords Recovery.bat

@echo off & setlocal enabledelayedexpansion
Set "Copyright=by Hackoo 2017"
Title  %~n0 %Copyright%
Mode con cols=75 lines=8
cls & color 0A & echo.
    echo             ***********************************************
    echo                 %~n0 %Copyright%
    echo             ***********************************************
    echo(
if _%1_==_Main_  goto :Main
Set Count=0
Set L=0
:getadmin
    echo               %~nx0 : self elevating
    set vbs=%temp%\getadmin.vbs
(
    echo Set UAC = CreateObject^("Shell.Application"^)
    echo UAC.ShellExecute "%~s0", "Main %~sdp0 %*", "", "runas", 1
)> "%vbs%"
    "%temp%\getadmin.vbs"
    del "%temp%\getadmin.vbs"
goto :eof
::*************************************************************************************
:Main
Call :init
Call :CountLines
Set "PasswordLog=%~dp0Wifi_Passwords_on_%ComputerName%.txt"
%Mod%
    echo(
    echo             ***********************************************
    echo                 %~n0 %Copyright%
    echo             ***********************************************
    echo(
Call :Color 0E "                 [N][SSID] ================ Password" 1
echo(
(
    echo             ***********************************************
    echo                 %~n0 %Copyright%
    echo             ***********************************************
    echo(
    echo                  [N][SSID] ==============^> "Password"
    echo(

)>"%PasswordLog%"
for /f "skip=2 delims=: tokens=2" %%a in ('netsh wlan show profiles') do (
    if not "%%a"=="" (
        set "ssid=%%a"
        set "ssid=!ssid:~1!"
        call :Getpassword "!ssid!"
    )
)
echo(
echo Done
If exist "%PasswordLog%" start "" "%PasswordLog%"
pause>nul
exit
::*************************************************************************************
:Getpassword
set "name=%1"
set "name=!name:"=!"
Set "passwd="
for /f "delims=: tokens=2" %%a in ('netsh wlan show profiles %1 key^=clear ^|find /I "Cont"') do (
    set "passwd=%%a"
    Set /a Count+=1
)

If defined passwd (
    set passwd=!passwd:~1!
    echo                  [!Count!][!name!] ====^> "!passwd!"
    echo                  [!Count!][!name!] ====^> "!passwd!" >> "%PasswordLog%"
) else (
    Set /a Count+=1
call :color 0C "                 [!Count!][!name!] The Password is empty" 1
    echo                  [!Count!][!name!] The Password is empty >> "%PasswordLog%"
)
exit /b
::*************************************************************************************
:init
prompt $g
for /F "delims=." %%a in ('"prompt $H. & for %%b in (1) do rem"') do set "BS=%%a"
exit /b
::*************************************************************************************
:color
set nL=%3
if not defined nL echo requires third argument & pause > nul & goto :eof
if %3 == 0 (
    <nul set /p ".=%bs%">%2 & findstr /v /a:%1 /r "^$" %2 nul & del %2 2>&1 & goto :eof
) else if %3 == 1 (
    echo %bs%>%2 & findstr /v /a:%1 /r "^$" %2 nul & del %2 2>&1 & goto :eof
)
exit /b
::*************************************************************************************
:CountLines
for /f "skip=2 delims=: tokens=2" %%a in ('netsh wlan show profiles') do (
    if not "%%a"=="" (
        set /a L+=1
    )
)
set /a L=!L! + 10
Set Mod=Mode con cols=75 Lines=!L!
exit /b
::*************************************************************************************

Output results like this one : enter image description here

Hackoo
  • 1
  • 9
  • 22
0

Expected output in key.txt file is

password@1

Use the following batch file (test.cmd):

@echo off
setlocal enabledelayedexpansion
netsh wlan show profile MyWiFi-name key=clear >tmp.txt
for /f "tokens=2 delims=:" %%i in ('type tmp.txt ^| findstr "Key Content"') do (
  set _key=%%i
  set _key=!_key:~1!
  echo !_key! > key.txt
  )
endlocal

Example:

> test

> type key.txt
password@1

Further Reading

DavidPostill
  • 153,128
  • 77
  • 353
  • 394
-1
netsh wlan show profile (Network-Name) key=clear | findstr /C:"Key Content" > key.txt 
FOR /F "tokens=4 delims= " %%i in (key.txt) do @echo %%i > key.txt

All key.txt examples should be pathed to where you want the text file with the key to be.