Skip to content

Instantly share code, notes, and snippets.

@tnhung2011
Last active December 24, 2025 17:28
Show Gist options
  • Select an option

  • Save tnhung2011/81b402d2776700b63ee3f0141c96b072 to your computer and use it in GitHub Desktop.

Select an option

Save tnhung2011/81b402d2776700b63ee3f0141c96b072 to your computer and use it in GitHub Desktop.
Creates a scheduled task that runs Mem Reduct with administrative privileges (skipping UAC) on logon. Alternative to faulty GUI option.
<!-- : Begin batch script
@echo off
setlocal enableextensions enabledelayedexpansion
(set lf=^
)
::net session would not work if the Server service is not started
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 allUsers=/ru Users
if errorlevel 2 (
set "taskname=%taskname% for %username%"
set allUsers=
)
if not exist "%systemroot%\system32\tasks\%taskname%\" if exist "%systemroot%\system32\tasks\%taskname%" (
choice /M "The task name \"%taskname%\" already exists. Do you want to replace it"
if errorlevel 2 goto end
)
set exe=
(for /f "tokens=2* skip=2" %%a in ('reg query HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\memreduct /v installlocation') do (
echo Installation entry found.
if exist "%%~b\memreduct.exe" (
echo File found: %%~b\memreduct.exe
set "exe=%%~b\memreduct.exe"
) else (echo File not found: %%~b\memreduct.exe)
)) || echo No installation entry found.
if defined exe (
choice /T 10 /D n /M "Do you want to specify another executable (timeout: 10s)"
if errorlevel 255 goto end
if errorlevel 2 goto create
) else (
echo Please specify an executable below.
)
set /p "exe=Path: "|| (echo Input empty.& goto end)
::delayed expansion ensures that quotation marks are passed through
for /f "tokens=*" %%a in ("!exe!") do set "exe=%%~fa"
::this if-statement seem to work when %%~fa expands to "C:\path\to\folder\." (whereas "." is a wildcard)
::confirmed for Windows Server 2003
if exist "%exe%\" goto _filenotfound
if not exist "%exe%" (
:_filenotfound
echo File not found.
goto end
)
:create
schtasks /create /tn "%taskname%" /sc ONLOGON /tr "'%exe%'" /rl HIGHEST %allUsers% /f >nul || goto _cantcreate
cscript //nologo "%~f0?.wsf" || goto _cantcreate
echo Successfully created task "%taskname%"
goto end
:_cantcreate
echo An error occured while creating task "%taskname%".
goto end
:delete
choice /M "Are you sure"
if errorlevel 2 goto end
set tempfile="%temp%\Mem Reduct scheduled tasks"
dir /b /a:-d "%systemroot%\system32\tasks\Mem Reduct*" >%tempfile% 2>nul || goto notasks
set delall=
(for /f %%a in ('type %tempfile% ^| find /v "" /c') do (
if "%%a"=="1" (set delall=1) else (if "%%a"=="0" (
:notasks
echo No tasks starting with "Mem Reduct*" found.
goto end
))
echo Number of tasks: %%a
)) || goto end
if defined delall goto _del
echo Specify tasks to delete...
echo Current user for reference: %username%
"%systemroot%\notepad.exe" %tempfile%
choice /M "Finished editing" /C y
if errorlevel 255 goto end
:_del
for /f "usebackq tokens=*" %%b in (%tempfile%) do schtasks /delete /tn "%%b" /f
del %tempfile%
: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
taskdef.Settings.IdleSettings.StopOnIdleEnd = false
'Normal priority (4-6)
taskdef.Settings.Priority = 4
'Indefinite time limit
taskdef.Settings.ExecutionTimeLimit = "PT0S"
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