Codebox Software

Run as System User


By default Windows is configured to run any tasks scheduled with the 'at' command as the System user, rather than as the user that scheduled the command. This can be a useful way of performing tasks that require System privileges, although you usually need to be a local administrator to run 'at' in the first place. The script takes any Windows command that you give it, and schedules it to run 1 minute in the future, then all you have to do is wait for the new Command window to pop open and the task will be running under the System account.

@echo off

set CMD=%*
if "%CMD%"=="" (
    echo Please specify a command to execute
    echo Usage: runsys.bat COMMAND
    goto EXIT

for /f %%i in ('time /t') do set TIME=%%i
set HH1=%TIME:~0,1%
set HH2=%TIME:~1,1%
set HH=%HH1%%HH2%
set MM1=%TIME:~3,1%
set MM2=%TIME:~4,1%
set MM=%MM1%%MM2%

if "%MM%"=="59" (
    if "%HH%"=="23" (
        set RUNHH=00
    ) else (
        if "%HH1%"=="0" (
            set /a RUNHH=%HH2%+1
        ) else (
            set /a RUNHH=%HH%+1
    set RUNMM=00
) else (
    set RUNHH=%HH%
    if "%MM1%"=="0" (
        set /a RUNMM=%MM2%+1
    ) else (
        set /a RUNMM=%MM%+1

at %RUNHH%:%RUNMM% /interactive %CMD%
echo Command will run at %RUNHH%:%RUNMM%, please wait...


Most of the code above is concerned with correctly calculating the time at which the command should be run, making sure the hours and minutes roll over properly if you happen to run the script at 23:59, for example. Extra pain is caused by the fact that the 'SET /A' command treats any numbers beginning with a '0' as octal, and therefore will error if you supply '08' or '09'. The interesting stuff happens near the end, where we actually call the scheduler, the '/interactive' switch is required if we want to see the results of the command on the desktop, and can be omitted if the task can just run in the background.

To use the script, just supply the command that you wish to run as a command-line argument, there is no need to enclose the command in quotes even if it has its own argument list:

runsys.bat echo I AM SYSTEM
runsys.bat taskkill /f /im mcshield.exe