Category filter
Script to connect to Wi-Fi on Windows devices
Organizations heavily rely on stable and secure network connections to ensure seamless communication and data transfer across devices. For IT administrators managing a fleet of Windows devices, connecting these devices to a specific Wi-Fi network is a requirement. Moreover, devices connected to the same network can easily share resources such as printers, servers, and files.
This document provides PowerShell scripts to connect Windows devices to a specified Wi-Fi network, either an existing known network or a new one. The script can be deployed to Windows devices using Hexnode UEM’s Execute Custom Script action.
PowerShell script to connect the device to a specific Wi-Fi network
For a known Wi-Fi network:
1 |
netsh wlan connect name="<Name of the Wi-Fi network>" |
For example: netsh wlan connect name="Wi-Fi01"
“netsh” is a command-line utility used for configuring and managing network-related settings on Windows. The “netsh wlan connect” command is used to connect to a specified Wi-Fi network. Replace “<Name of the Wi-Fi network>” with the actual name (SSID) of the Wi-Fi network you want to connect to.
For connecting to a new Wi-Fi network for the first time:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
$ProfileName = "<SSID>" # Replace with the actual SSID of the Wi-Fi network $Password = "<Password>" # Replace with the Wi-Fi password # Create a Wi-Fi profile $Profile = @" <?xml version="1.0"?> <WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1"> <name>$ProfileName</name> <SSIDConfig> <SSID> <name>$ProfileName</name> </SSID> </SSIDConfig> <connectionType>ESS</connectionType> <connectionMode>manual</connectionMode> <MSM> <security> <authEncryption> <authentication>WPA2PSK</authentication> <encryption>AES</encryption> <useOneX>false</useOneX> </authEncryption> <sharedKey> <keyType>passPhrase</keyType> <protected>false</protected> <keyMaterial>$Password</keyMaterial> </sharedKey> </security> </MSM> </WLANProfile> "@ # Export the profile to an XML file $Profile | Out-File -FilePath "$env:USERPROFILE\$ProfileName.xml" # Add the profile to the Wi-Fi interface netsh wlan add profile filename="$env:USERPROFILE\$ProfileName.xml" # Connect to the Wi-Fi network netsh wlan connect name="$ProfileName" |
The script assists users in connecting their Windows device to a new Wi-Fi network using PowerShell. It starts by specifying the network’s name (SSID) and password. The script then generates a Wi-Fi profile with these details, saving it as an XML file. Utilizing ‘netsh‘, the profile is added to the Wi-Fi settings, and ultimately, the script connects the device to the designated Wi-Fi network.
The script employs a heredoc (@” … “@) to create a Wi-Fi profile, defining essential parameters for configuring the Wi-Fi connection. A here-document (heredoc) enables the inclusion of multiline strings within a script without the need for explicit concatenation. The <name> and <SSID> elements are set to the specified $ProfileName. Additionally, <connectionType> is configured as ‘ESS‘ for an Infrastructure BSS network, and <connectionMode> is set to ‘manual‘, indicating a manual connection mode. In the security settings, <authEncryption> specifies WPA2PSK encryption with AES, ensuring a secure Wi-Fi connection. <sharedKey> contains the Wi-Fi password provided in $Password.
What happens at the device end?
With the execution of this script, the currently connected Wi-Fi network will be disconnected and replaced by the specified Wi-Fi network.
The output for the script execution can be verified from the Action History tab of the device.