Automating with PowerShell: Deploying WiFi Profiles

I’m working with one of my collaborators to create scripts for their RMM system and they got a request from one of their clients “Can we add WiFi profiles using PowerShell?” and of course I pointed them to my blog. Only to find to my shame that I never blogged about it.

Adding WiFi profiles is very straight forward when you’re using a domain environment, you simply create a GPO and click around in the settings until you’ve perfected your setup, but in modern styled environments it’s not always that easy, and of course you want to use your RMM system to deploy the correct Wi-Fi Settings to all users.

So, let’s jump into that; today we’re adding a Wifi Connecting that uses a WPA2 PSK with AES encryption – The most commonly used network these days.

The Script

You’ll have to set $SSID and $PSK to your prefered SSID and PSK, remember that the SSID is case sensitive too.

param(
    [string]$SSID,
    [string]$PSK
)
$guid = New-Guid
$HexArray = $ssid.ToCharArray() | foreach-object { [System.String]::Format("{0:X}", [System.Convert]::ToUInt32($_)) }
$HexSSID = $HexArray -join ""
@"
<?xml version="1.0"?>
<WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
	<name>$($SSID)</name>
	<SSIDConfig>
		<SSID>
			<hex>$($HexSSID)</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>$($PSK)</keyMaterial>
			</sharedKey>
		</security>
	</MSM>
	<MacRandomization xmlns="http://www.microsoft.com/networking/WLAN/profile/v3">
		<enableRandomization>false</enableRandomization>
		<randomizationSeed>1451755948</randomizationSeed>
	</MacRandomization>
</WLANProfile>
"@ | out-file "$($ENV:TEMP)\$guid.SSID"

netsh wlan add profile filename="$($ENV:TEMP)\$guid.SSID" user=all

remove-item "$($ENV:TEMP)\$guid.SSID" -Force

And that’s it! using this script you can deploy Wi-Fi to your clients with relative ease 🙂 As always, Happy PowerShelling.

6 Comments

  1. Michael McCool April 21, 2021 at 4:52 pm

    Here is a Windows 7 compatible version of the XML. If you support Windows 7 devices still, the above will likely not work with them. This XML will work for both Windows 7 and 10.

    $($SSID)

    $($HexSSID)
    $($SSID)

    ESS
    auto

    WPA2PSK
    AES
    false

    passPhrase
    false
    $($PSK)

  2. Jimbo June 28, 2021 at 3:31 pm

    Thanks for this! It solved my problem nicely. As post-lockdown (ish) people are starting to work in our office again, users haven’t been able to connect to the office wifi for various reasons. I can’t give them the password (company policy), nor can I do it via remote access to their machines, as configuring a new connection kills the current connection.
    Top tip – the wifi password I’ve been dealing with has an ampersand in it (&), which breaks your script. The solution was to replace it with the html special character &

  3. Jimbo June 28, 2021 at 3:36 pm

    Ugh – auto-formatting on my comment above has mucked up my explanation! The very last character is not a simple & – instead of me fiddling about, search in Google for ‘html special characters &’ and you’ll see the string I meant under the ‘Replacement’ column

  4. stephen July 21, 2021 at 9:26 pm

    Awesome script working perfectly.

    Just curious, could you shed some insight on how to edit this to add multiple WiFI profiles?

    I found that I could wrap the entire code block into a Function, create a function for each wifi profile, then call each function and it works as intended.

    I’m just interested in how you could clean it up. Great stuff!

  5. jmac August 18, 2021 at 5:57 pm

    I must be a bit dumb/blind but where do you actually set the SSID and PSK in the script?

    1. Fred September 3, 2021 at 9:40 pm

      This seems to work for me:

      param(
      [string]$SSID = “PUT YOUR SSID HERE AND INCLUDE THE QUOTATION MARKS”,
      [string]$PSK = “PUT YOUR PASSWORD HERE AND INCLUDE THE QUOTATION MARKS”
      )

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.