12

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

cSteusloff
  • 2,487
  • 7
  • 30
  • 51

3 Answers3

23

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.

cSteusloff
  • 2,487
  • 7
  • 30
  • 51
  • 3
    N.B. If you want to manually edit the config file, make sure to update the element inside . 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 ''` – Per von Zweigbergk Feb 19 '19 at 15:24
  • 2
    @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: https://learn.microsoft.com/en-us/windows/win32/nativewifi/wlan-profileschema-hex-ssid-element – Jimmy Westberg May 20 '21 at 07:13
6

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
Ross
  • 186
  • 1
  • 8
0

I used what was here, but got an error when I tried to add the profile because our password contains an "invalid character". (The error I got was "An attempt was made to load a program with an incorrect format." and found out what caused it after a Google search.) So I added some logic to replace those unacceptable characters and make them acceptable.

It also forces the XML file to be created in the same folder as the script (for some reason it gave me problems without this.)

Lastly, I added a (commented out) user=all to the command, which can be easily uncommented in case you want to add this profile for all users instead of just the user running the script. (Which I am using.)

$profilefile = "ACprofile.xml"
$SSID = "WifiNetwork"
$PW = "Password1234"

function Get-ScriptPath()
{
    # If using PowerShell ISE
    if ($psISE)
    {
        $ScriptPath = Split-Path -Parent -Path $psISE.CurrentFile.FullPath
    }
    # If using PowerShell 3.0 or greater
    elseif($PSVersionTable.PSVersion.Major -gt 3)
    {
        $ScriptPath = $PSScriptRoot
    }
    # If using PowerShell 2.0 or lower
    else
    {
        $ScriptPath = split-path -parent $MyInvocation.MyCommand.Path
    }

    # If still not found
    # I found this can happen if running an exe created using PS2EXE module
    if(-not $ScriptPath) {
        $ScriptPath = [System.AppDomain]::CurrentDomain.BaseDirectory.TrimEnd('\')
    }

    # Return result
    return $ScriptPath
}

# Replace Unacceptable values in password
function FormatKey($key) {
    $key = $key.replace("""", "&quot;")
    $key = $key.replace("&", "&amp;")
    $key = $key.replace("'", "&pos;")
    $key = $key.replace("<", "&lt;")
    $key = $key.replace(">", "&gt;")
    return $key
}

$PW = FormatKey($PW)
$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>
"

#Create XML File
$XMLFILE > "$(Get-ScriptPath)\$profilefile"

#Create, display, then connect to the new network
netsh wlan add profile filename="$(Get-ScriptPath)\$profilefile" #user=all
netsh wlan show profiles $SSID key=clear
netsh wlan connect name=$SSID

# Delete the XML file we created
Remove-Item "$(Get-ScriptPath)\$profilefile" -Force
Randy
  • 1,068
  • 2
  • 14
  • 32