Programmably connect to a WiFi network using a password

Connecting to a wireless network using a password is harder than what you might think, as you need to create a WiFi ‘profile’.

The below PowerShell code can do exactly what you need, it creates the WiFi profile, and connects you to the WiFi network automatically. 

# Fill in mandatory details for the WiFi network
$WirelessNetworkSSID = '<WiFi_SSID>'
$WirelessNetworkPassword = '<WiFi_password>'
$Authentication = 'WPA2PSK' # Could be WPA2
$Encryption = 'AES'
# Create the WiFi profile, set the profile to auto connect
$WirelessProfile = @'
<WLANProfile xmlns=""&gt;
'@ -f $WirelessNetworkSSID, $WirelessNetworkPassword, $Authentication, $Encryption
# Create the XML file locally
$random = Get-Random Minimum 1111 Maximum 99999999
$tempProfileXML = "$env:TEMP\tempProfile$random.xml"
$WirelessProfile | Out-File $tempProfileXML
# Add the WiFi profile and connect
Start-Process netsh ('wlan add profile filename={0}' -f $tempProfileXML)
# Connect to the WiFi network – only if you need to
Start-Process netsh ('wlan connect name="{0}"' -f $WirelessNetworkSSID)

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s