Add wifi profile with password in windows programmatically

46,894

Solution 1

I found a way to add a wifi profile.

At first you export an existing wifi profile:

netsh wlan export profile name="WifiNetwork" folder="C:\path\" key=clear

Than you get a XML file with the following style:

<?xml version="1.0"?>
<WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
    <name>WifiNetwork</name>
    <SSIDConfig>
        <SSID>
            <hex>576966694E6574776F726B</hex>
            <name>WifiNetwork</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>Password123</keyMaterial>
            </sharedKey>
        </security>
    </MSM>
</WLANProfile>

Than you can modify this file and import it to add this wifi with this command:

netsh wlan add profile filename="C:\path\WifiNetwork.xml"

Check your profiles with:

netsh wlan show profile

Check your profile with key:

netsh wlan show profiles WifiNetwork key=clear

I hope I could help someone with this information.

Solution 2

I wrote a power shell script - the first three lines in the following code havent been tested as in my script I get it from a CSV file - the rest is as is - and works on the two SSIds I have

$profilefile="ACprofile.xml"
$SSID="ACSSID"
$PW="12345678"

$SSIDHEX=($SSID.ToCharArray() |foreach-object {'{0:X}' -f ([int]$_)}) -join''
$xmlfile="<?xml version=""1.0""?>
<WLANProfile xmlns=""http://www.microsoft.com/networking/WLAN/profile/v1"">
    <name>$SSID</name>
    <SSIDConfig>
        <SSID>
            <hex>$SSIDHEX</hex>
            <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>$PW</keyMaterial>
            </sharedKey>
        </security>
    </MSM>
</WLANProfile>
"

$XMLFILE > ($profilefile)
netsh wlan add profile filename="$($profilefile)"
netsh wlan show profiles $SSID key=clear
netsh wlan connect name=$SSID
Share:
46,894
cSteusloff
Author by

cSteusloff

The way from PHP and MySQL to ASP.Net (C#) and MSSQL

Updated on October 10, 2020

Comments

  • cSteusloff
    cSteusloff over 3 years

    Is it possible to programmatically add a wifi profile to a windows operating system (minimum version windows 7)?

    I tried netsh with add profile and connect, but it doesn't work for me. Is there any powershell commands which does this?

    I want to set a wifi password for a special ssid for many clients automatically.

    I hope someone has an idea or can give me a command with this sample information:

    • SSID: WifiNetwork
    • Password: Password123

    Thanks

  • Per von Zweigbergk
    Per von Zweigbergk about 5 years
    N.B. If you want to manually edit the config file, make sure to update the <hex> element inside <ssidconfig><ssid>. It needs to be a HEX representation of the SSID. Powershell can be used to generate this: ("WifiNetwork".ToCharArray() |foreach-object {'{0:X}' -f ([int]$_)}) -join ''
  • Maxxik CZ
    Maxxik CZ almost 4 years
    even when I included key=clear, the password was still encrypted. After closing cmd, deleting xml files and restarting netsh it finally worked
  • XLR8
    XLR8 over 3 years
    dont forget to change variables SSID and password in script,it won`t ask for input
  • Jimmy Westberg
    Jimmy Westberg almost 3 years
    @PervonZweigbergk the hex OR name is optional. "Although the hex and name elements are optional, at least one hex or name element must appear as a child of the SSID element." reference: docs.microsoft.com/en-us/windows/win32/nativewifi/…
  • Per von Zweigbergk
    Per von Zweigbergk almost 3 years
    @JimmyWestberg Ah cool, so then I assume you can just delete the hex element and call it a day?
  • Jimmy Westberg
    Jimmy Westberg almost 3 years
    @PervonZweigbergk yep, simple and straight forward.