Add wifi profile with password in windows programmatically

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.


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