Created
December 7, 2025 15:53
-
-
Save tnhung2011/81b402d2776700b63ee3f0141c96b072 to your computer and use it in GitHub Desktop.
Creates a task that runs Mem Reduct with administrative privileges on logon.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!-- : Begin batch script | |
| @echo off | |
| setlocal enableextensions enabledelayedexpansion | |
| (set lf=^ | |
| ) | |
| reg query HKEY_USERS\S-1-5-19\Environment /v TEMP >nul 2>nul || ( | |
| choice /M "Re-run the script as administrator?" | |
| if errorlevel 2 goto :eof | |
| mshta vbscript:Execute("CreateObject(""Shell.Application"").ShellExecute ""%~f0"", """", """", ""runas"", 1:code close"^) | |
| goto :eof | |
| ) | |
| set "taskname=Mem Reduct" | |
| choice /c 123 /M "!lf!Run Mem Reduct at logon!lf!!lf![1] Create task for all users!lf![2] Create task for current user!lf![3] Delete task!lf!!lf!" | |
| if errorlevel 255 goto end %= "The file is either empty or does not contain the valid choices." =% | |
| if errorlevel 3 goto delete | |
| set ru=/ru Users | |
| if errorlevel 2 ( | |
| set "taskname=%taskname% for %username%" | |
| set ru= | |
| ) | |
| dir /b "%systemroot%\system32\tasks\%taskname%" >nul 2>nul && ( | |
| choice /M "The task name \"%taskname%\" already exists. Do you want to replace it" | |
| if errorlevel 2 goto end | |
| ) | |
| set exe= | |
| set another= | |
| (for /f "tokens=2* skip=2" %%a in ('reg query HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\memreduct /v installlocation') do ( | |
| echo Querying %%~b... | |
| if exist "%%~b\memreduct.exe" ( | |
| echo memreduct.exe found^^! | |
| set "exe=%%~b\memreduct.exe" | |
| set another= another | |
| ) else (echo memreduct.exe not found.) | |
| )) || echo No installation entries found. | |
| choice /T 10 /D n /M "Specify%another% executable (timeout: 10s for N)" | |
| if errorlevel 255 goto end | |
| if errorlevel 2 if defined exe (goto create) else (goto end) | |
| set /p "exe=Path: "|| (echo Input empty. & goto end) | |
| ::check folder exists (folder should not have the same name as file) | |
| if exist "%exe%\" goto _filenotfound | |
| if not exist "%exe%" ( | |
| :_filenotfound | |
| echo File not found. | |
| goto end | |
| ) | |
| :create | |
| schtasks /create /tn "Mem Reduct" /sc ONLOGON /tr "'%%~b\memreduct.exe'" /rl HIGHEST %ru% /f >nul || goto end | |
| cscript //nologo "%~f0?.wsf" || goto end | |
| echo Successfully created task "%taskname%" | |
| goto end | |
| :delete | |
| choice /M "Are you sure?" | |
| if errorlevel 2 goto end | |
| set tempfile="%temp%\Mem Reduct scheduled tasks" | |
| dir /b "%systemroot%\system32\tasks\Mem Reduct*" >%tempfile% 2>nul || goto notasks | |
| set delall= | |
| (for /f %%a in ('find /v "" /c %tempfile%') do if "%%a"=="1" (set delall=1) else (if "%%a"=="0" ( | |
| :notasks | |
| echo No tasks starting with "Mem Reduct*" found. | |
| goto end | |
| ))) || goto end | |
| if defined delall goto _del | |
| echo Specify tasks to delete. | |
| echo Current user for reference: %username% | |
| echo Starting Notepad... | |
| "%systemroot%\notepad.exe" %tempfile% | |
| :_del | |
| for /f "usebackq tokens=*" %%b in (%tempfile%) do schtasks /delete /tn "%%b" /f | |
| :end | |
| pause | |
| goto:eof | |
| ----- Begin wsf script ---> | |
| <job><script language="VBScript"> | |
| taskname = CreateObject("WScript.Shell").Environment("Process").Item("taskname") | |
| 'https://learn.microsoft.com/en-us/windows/win32/taskschd/time-trigger-example--scripting-?redirectedfrom=MSDN | |
| 'https://learn.microsoft.com/en-us/windows/win32/taskschd/task-scheduler-objects | |
| Set service = CreateObject("Schedule.Service") | |
| call service.Connect() | |
| Set rootFolder = service.GetFolder("\") | |
| Set taskdef = rootFolder.GetTask(taskname).Definition | |
| taskdef.Settings.DisallowStartIfOnBatteries = false | |
| taskdef.Settings.StopIfGoingOnBatteries = false | |
| taskdef.Settings.StartWhenAvailable = true | |
| 'Normal priority (4-6) | |
| taskdef.Settings.Priority = 4 | |
| taskdef.Settings.ExecutionTimeLimit = vbNullString | |
| call rootFolder.RegisterTaskDefinition(taskname, taskdef, 6, , , 3) | |
| </script></job> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment