[Solved] Need help creating a "toggle" script

Discussion in 'Scripting' started by freddie-o, Jan 31, 2019.

  1. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,358
    2,267
    60
    #1 freddie-o, Jan 31, 2019
    Last edited: May 13, 2019
    MOVED SOLUTION HERE: Windows Defender Auto Toggle





    Does somebody know how to create a "toggle" script, (either a .bat or .ps1) for these 2 commands?

    Code:
    DefenderControl.exe /D
    
    DefenderControl.exe /E

    Edit:
    They're command line switches for Defender Control

    [​IMG]



    So if Windows Defender is enabled and I run the script, Windows Defender gets disabled. And vice versa

    Thanks
     
  2. drew84

    drew84 MDL Expert

    Mar 13, 2014
    1,347
    2,302
    60
    #1233 might be what you are looking for
     
  3. murphy78

    murphy78 MDL DISM Enthusiast

    Nov 18, 2012
    7,394
    11,615
    240
    I had something kinda similar for the registry override key that I use. You're welcome to do whatever you want with it.
    Code:
    @echo off
    
    title Disable/Enable Windows Defender
    color 1f
    :Begin UAC check and Auto-Elevate Permissions
    :-------------------------------------
    REM  --> Check for permissions
    >nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
    
    REM --> If error flag set, we do not have admin.
    if '%errorlevel%' NEQ '0' (
    echo:
    echo   Requesting Administrative Privileges...
    echo   Press YES in UAC Prompt to Continue
    echo:
    
        goto UACPrompt
    ) else ( goto gotAdmin )
    
    :UACPrompt
        echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
        echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
    
        "%temp%\getadmin.vbs"
        exit /B
    
    :gotAdmin
        if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
        pushd "%CD%"
        CD /D "%~dp0"
    :--------------------------------------
    
    :Check the key:
    (reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows Defender" /v "DisableAntiSpyware"|find /i "0x1")>NUL 2>NUL
    if %errorlevel% neq 0 GOTO :KEYOFF
    
    :KEYON
    echo ============================================================
    echo Windows Defender currently disabled.
    echo Would you like to re-enable it? (Y/N)
    echo ============================================================
    echo.
    choice /c yn /n
    If %ERRORLEVEL% NEQ 1 GOTO :QUIT
    
    Echo Changing Registry key
    REG ADD "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows Defender" /v "DisableAntiSpyware" /D 0 /T REG_DWORD /F>NUL 2>NUL
    IF %ERRORLEVEL% NEQ 0 GOTO :ERROR
    Echo.
    
    Echo Windows Defender enabled
    Echo.
    goto :QUIT
    
    
    :KEYOFF
    echo ============================================================
    echo Windows Defender is currently enabled.
    echo Would you like to disable it? (Y/N)
    echo ============================================================
    echo.
    choice /c yn /n
    If %ERRORLEVEL% NEQ 1 GOTO :QUIT
    
    Echo Changing Registry key
    REG ADD "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows Defender" /v "DisableAntiSpyware" /D 1 /T REG_DWORD /F>NUL 2>NUL
    IF %ERRORLEVEL% NEQ 0 GOTO :ERROR
    Echo.
    
    Echo Windows Defender disabled
    Echo.
    goto :QUIT
    
    
    :QUIT
    echo ============================================================
    echo Press any key to exit...
    echo ============================================================
    pause>NUL
    goto :EOF
    
    :ERROR
    echo ============================================================
    echo The script ran into an unexpected error setting reg key.
    echo Press any key to exit...
    echo ============================================================
    pause>NUL
    goto :EOF
     
  4. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,358
    2,267
    60
    Thanks, that's what I'm using right now (that's actually my post if you didn't notice :) )
     
  5. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,358
    2,267
    60
    #5 freddie-o, Jan 31, 2019
    Last edited: Jan 31, 2019
    (OP)

    Thanks but the script didn't work on LTSC 2019. Anyway I'm using a toggle switch right now which drew84 mentioned. But lately I experienced that when I disable Windows Defender with that toggle switch, Defender still quarantined a false positive. I think that Defender Control might be able to do a better job at disabling Defender. It will work with command lines "DefenderControl.exe /D" and "DefenderControl.exe /E". I want to add the toggle script to my Context menu to quickly disable Defender.
     
  6. murphy78

    murphy78 MDL DISM Enthusiast

    Nov 18, 2012
    7,394
    11,615
    240
    Ah strange. I've used it on LTSC 2019 before just fine. Maybe there's a delay before the system recognizes it or you need to reboot or something?
     
  7. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,358
    2,267
    60
    #7 freddie-o, Feb 2, 2019
    Last edited: Feb 3, 2019
    (OP)
    Anyway I need something that will disable Defender quickly, before it quarantines or deletes a file. That's why a toggle script on the context menu.
     
  8. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,358
    2,267
    60
    #8 freddie-o, Feb 2, 2019
    Last edited: Feb 3, 2019
    (OP)
    Found a temp solution. Defender Control has the option to minimize on the system tray so Defender can be disabled and enabled from there.