7

How to connect to a new WiFi by entering a password using CMD?

For my school project I have decided to make a WiFi_manager program using cmd.

I know to display all WiFi networks (in cmd):

netsh wlan show networks

Now let's say I want to connect to a WiFi network that I never connected to before. And that WiFi is not added any profiles also.

But I know the password for the WiFi.

What will be the command line for that.

Given the information on WiFi network below:

SSID 3 : Ismail
    Network type            : Infrastructure
    Authentication          : WPA-Personal
    Encryption              : CCMP

and password is "Thanks_bro".

I search this question already in google but none of them say the right way and most of them are related to hacking and connecting a WiFi without password etc. So I posted this question to the BEST programmers out here to answer!

If not possible, can we do using C++?

2 Answers 2

6

Unfortunately if you want to connect to a wireless network from command prompt that requires a passcode it won't happen.

First of all to connect to a wireless network with a password you need to setup a profile or you need to have an already set up profile you can check that profile by using

Netsh wlan show profiles

To see your profile you need to be connected to that wifi at least once .

The alternative way is to have a generated created xml profile and use the following command to connect to it :

netsh wlan connect ssid=YOURSSID name=PROFILENAME interface="WIRELESS NETWORK CONNECTION"

More info could be found here in this article

1
4

Fortunately, you can easily connect to a password-protected wireless network using only the command prompt.

First, create an XML file with your Wi-Fi connection details. Use this as a template, replacing {SSID} (appears twice) and {PASSWORD} with your own:

<?xml version="1.0"?>
<WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
    <name>{SSID}</name>
    <SSIDConfig>
        <SSID>
            <name>{SSID}</name>
        </SSID>
    </SSIDConfig>
    <connectionType>ESS</connectionType>
    <connectionMode>auto</connectionMode>
    <MSM>
        <security>
            <authEncryption>
                <authentication>WPA2PSK</authentication>
                <encryption>AES</encryption>
                <useOneX>false</useOneX>
            </authEncryption>
            <sharedKey>
                <keyType>passPhrase</keyType>
                <protected>false</protected>
                <keyMaterial>{PASSWORD}</keyMaterial>
            </sharedKey>
        </security>
    </MSM>
    <MacRandomization xmlns="http://www.microsoft.com/networking/WLAN/profile/v3">
        <enableRandomization>false</enableRandomization>
    </MacRandomization>
</WLANProfile>

Next, create your batch file (again, replace {SSID} with your own):

@echo off
netsh wlan add profile filename="{whatever you named the file}.xml"
netsh wlan connect name="{SSID}"
echo.
pause

Inspired by this Q&A here (and my own need to do this today):
https://superuser.com/a/1275748/243707


But why stop there?
Combine the whole thing into a single script, and clean up any XML clutter afterwards.

@echo off

set "SSID=enter your network name here"
set "HEXSTR=enter your password here"
set "XML_OUTPUT_PATH=%TEMP%\%SSID%-wireless-profile-generated.xml"

echo ^<?xml version="1.0"?^>^<WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1"^>^<name^>%SSID%^</name^>^<SSIDConfig^>^<SSID^>^<name^>%SSID%^</name^>^</SSID^>^</SSIDConfig^>^<connectionType^>ESS^</connectionType^>^<connectionMode^>auto^</connectionMode^>^<MSM^>^<security^>^<authEncryption^>^<authentication^>WPA2PSK^</authentication^>^<encryption^>AES^</encryption^>^<useOneX^>false^</useOneX^>^</authEncryption^>^<sharedKey^>^<keyType^>passPhrase^</keyType^>^<protected^>false^</protected^>^<keyMaterial^>%HEXSTR%^</keyMaterial^>^</sharedKey^>^</security^>^</MSM^>^<MacRandomization xmlns="http://www.microsoft.com/networking/WLAN/profile/v3"^>^<enableRandomization^>false^</enableRandomization^>^</MacRandomization^>^</WLANProfile^> >%XML_OUTPUT_PATH%

netsh wlan add profile filename="%XML_OUTPUT_PATH%"
netsh wlan connect name="%SSID%"

del "%XML_OUTPUT_PATH%"
echo.
pause
1
  • This is the extended version of what the older answer suggests in the second part.
    – Daniel B
    Feb 15, 2023 at 18:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .