Search code examples
windowsbatch-filewirelessnetsh

if statements within batch scripts to deploy wireless profiles to windows computers using netsh


I would like to automate the deployment of wireless profiles.

The scenario is, there are Windows computers already connected to network A. I would like to deploy to all computers a new wireless profile for network B with the exception of those computers that are already connected to network A.

Below is a rough idea of what I think I need, however i'm struggling to find the correct code.

netsh wlan show profile | find "wireless profile name"
if exist "wireless profile name"
    do nothing
else
    netsh wlan add profile filename="2nd wireless profile"

I know the top line works, the part I am struggling with is the if statement and how to check the result of find with the exist function.


Solution

  • You don't need to use find.exe for this task, just use the returned ErrorLevel from asking to see the profile:

    "%__AppDir__%netsh.exe" WLAN Show Profiles Name="wireless profile name">NUL
    If ErrorLevel 1 "%__AppDir__%netsh.exe" WLAN Add Profile FileName="2nd wireless profile"
    

    If you are positive that the PC's you're running this on have the default entries under %PATH% and %PATHEXT%, then you could probably shorten that to:

    NetSH WLAN Show Profiles Name="wireless profile name">NUL
    If ErrorLevel 1 NetSH WLAN Add Profile FileName="2nd wireless profile"