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.

Disclaimer:

The sample scripts provided below are adapted from third-party open-source sites.

PowerShell script to connect the device to a specific Wi-Fi network

For a known 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:

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 Windows device will be connected to the Wi-Fi network as specified in the script to connect to Wi-Fi on Windows

The output for the script execution can be verified from the Action History tab of the device.

The Action History tab shows the output for the script to connect to Wi-Fi on Windows

Notes:

  • It is recommended to manually validate the script execution on a system before executing the action in bulk.
  • Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.

  • Sample Script Repository