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.
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.
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.
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/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.
1
u/TheYoinks May 06 '26
You can and should be doing this all through intune policy
2
u/[deleted] May 06 '26
[removed] — view removed comment