1

I'm running a batch file to remove all personal WiFi networks from a Windows 10 device. We previously had an issue where WiFi profiles were being made under the SYSTEM account and it does not allow you to remove the profile. That has since been fixed, but previous networks saved under users computers need to be removed.

I have the following script to run and it's working as expected, BUT:

@ECHO OFF
:: Checking for Admin (Right-click) Condition
CLS
NET SESSION > NUL
IF NOT %ERRORLEVEL% EQU 0 (
COLOR 4F
ECHO.
ECHO -----  WARNING! ERROR EXECUTING THE COMMAND  -----
ECHO.
ECHO.
ECHO You must right-click and select "Run as Administrator" from the pop-up menu
ECHO.
ECHO.
GOTO END
) ELSE (
CLS
)

setlocal
:PROMPT
ECHO Using this will delete ALL PERSONAL wifi networks.
ECHO Users will have to type in their password for their specific SSID when in range.
SET /P QUESTION=Are you sure? (Y/[N])?
IF /I "%QUESTION%" NEQ "Y" GOTO END
netsh wlan delete profile name=*
ECHO Wifi networks deleted successfully

:END
echo.
echo.
PAUSE

We have 2 GPO assigned wireless networks. They are pushed through our wireless network policy. Looking for a possible EXCLUDE method to allow me to exclude those two from attempting to be deleted since I'm running a wildcard for ALL wireless profiles (Special cases where users would have access to delete those). Something like this possible?

0

To expand on music2myear's answer, I would use something similar to the following:

FOR /F "tokens=3* delims=: " %%a IN ('netsh wlan show profiles ^| FIND " : " ^| FINDSTR /v "My Wireless Network"') DO netsh wlan delete profile name="%%b"

You'll need to replace FINDSTR /v "My Wireless Network" with whatever find or findstr works in your case.

Breakdown

  • netsh wlan show profiles - List the profiles available on the computer.
  • FIND " : " - Filter our everything but the lines containing the profile SSID's.
  • FINDSTR /v "My Wireless Network" - Remove the exceptions from the list.
  • "tokens=3* delims=: " - Split each line into pieces and only pick the SSID’s.
  • ^| - The pipe needs to be escaped because it is inside of a for loop.

Further reading

0
1

Two ways I can think of:

Add logic to enumerate all of the currently configured wireless profiles on the computer, identify the two you wish to keep, and delete the others.

Force a gpupdate at the end of the script to re-add the profiles (note: this may require an active domain connection.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.