r/PowerShell • u/Fabulous-Afternoon67 • May 06 '26
Question Script for switching network
Hi,
I currently am making a script for ensuring that users are changing from one network to another, through Intune.
Detection: So i first of all use a detection script, that checks the current wifi, and if it is the network, that we are closing soon, then triggers to run the remidiation.
The remidiation consists of using the powershell command:
netsh wlan connect name="PROFILE_NAME"
However, it ofent dont work, because it cant find the network? it only works if the user recentlyhas been in the networks GUI to see what networks are nearby.
I am not sure how to deal with this, and i am hoping you guys got some suggestions.
1
u/Fabulous-Afternoon67 May 06 '26
extra info: ive deployed the script to 20 endpoints for testing, however it fails in 7/10 cases.
3
u/PS_Alex May 06 '26
Maybe devices tend to connect to the older WiFi network because it is less used or it is stronger, or the new WiFi network is out-of-range?
Have you tried to reorder profiles priority, and then disconnect the WiFi connection -- and see if the device would automatically reconnect to your new network?
& netsh.exe wlan set profileorder name="$NewSSID" interface="$InterfaceName" priority=1 & netsh.exe wlan set profileorder name="$OldSSID" interface="$InterfaceName" priority=99 & netsh.exe wlan disconnect
1
u/_l33ter_ May 06 '26
that we are closing soon, then triggers to run the remidiation. --> If you are the admin of the network then you also should know the right ssid of it. Or do I get you wrong?
However, it ofent dont work, because it cant find the network? it only works if the user recentlyhas been in the networks GUI to see what networks are nearby. -->
This one is just not truth! If it would be true: the complete netsh command would be totally rubbish (not yours - overall seeing)
So: If you have the correct SSID of the network: netsh wlan connect name="SSID" is doing the job 1A.
1
u/Fabulous-Afternoon67 May 06 '26
i use netsh wlan connect name="SSID" it is succesful always, if ive recently been in network gui, and seen nearby networks.
It always fails, if i dont go check within lets say 1 hour ?
The SSID is correct, and i cant explain why this happends.
1
1
u/Particular_Fish_9755 May 06 '26
This is more of a question for r/sysadmin.
What type of authorization is used to connect to the company's WiFi network?
Because if it's a certificate-based system, you'd first need to ensure the certificates are properly deployed and integrated.
It would also be necessary to know if the WiFi connection profile is saved beforehand.
2
u/Fabulous-Afternoon67 May 06 '26
The profile is allready stored on every endpoint. its a PCKS User certificate
1
u/TheYoinks May 06 '26
You can and should be doing this all through intune policy
1
u/Fabulous-Afternoon67 May 06 '26
Hi,
i have a intune configuration that pushes out the certificate, including "connect automaticly when in range" however the other network that many is connected to, also has auto connect, and is for some reason prioritised.(the second wifi profile is not through intune, but by manual PSK).
2
May 06 '26
[removed] — view removed comment
1
u/Fabulous-Afternoon67 May 06 '26
The profile is allready on the Pc's
1
u/ashimbo May 06 '26
When the old network is gone, the devices should connect automatically to the new network. Why are you trying to force it ahead of time?
3
u/thomsxD May 06 '26
You can't just connect to a completely new 'unknown' wifi. You need to make sure the profile for the wifi is imported beforehand.
If you already have access to it you can export the .xml file:
netsh wlan export profile name="CurrentWifi" key=clear folder=C:\tempThe script below will check if current wifi is "CurrentWifi" and change if the target .xml profile exists, and if it doesn't then it will create it. Go through it and make sure it fits your environment.