• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 871
  • Last Modified:

Schedule a defragment and system cleanup on a remote machine


I want to setup a defragment and system clean up on all the machines in the file.In which i have the machine names.I want to schedule it every 15 day to run on the machine.I want it to be done remotely.

1 Solution
Farhan KaziSystems EngineerCommented:
:: ================
:: ================
:: * To run this script you must have domain administrators rights.
:: * This script require "Computers.txt" file on C: drive root from where it will pick computer names.
:: * You need to set following variables value inside the script
::     DCTime <- Specifies the day of the week to run the task. Valid values: MON, TUE,WED, THU, FRI, SAT, SUN
::     DCTime <- specifies the start time to run disk cleanup task. The time format is HH:mm (24 hour time) Like: 14:30 for 2:30 PM.
::     DFTime <- specifies the start time to run disk defrag task. The time format is HH:mm (24 hour time) Like: 14:30 for 2:30 PM.
:: * Successful run will generate "MaintenanceRpt.txt" on C: drive root.
:: * Copy and Paste following script into notepad and save it with any name having .bat extension.
:: Batch Script Start

@Echo Off
SETLOCAL EnableDelayedExpansion
:: Day of the week to run the task (MON, TUE,WED, THU, FRI, SAT, SUN)
:: DiskCleanup Start Time (HH:mm)
SET DCTime=17:00
::Defrag Start Time  (HH:mm)
SET DFTime=18:00

IF NOT EXIST C:\Computers.txt Goto ShowErr
FOR %%R IN (C:\Computers.txt) Do IF %%~zR EQU 0 Goto ShowErr
IF EXIST C:\MaintenanceRpt.txt DEL /F /Q C:\MaintenanceRpt.txt

FOR /F %%c IN ('Type Computers.txt') Do (
    Echo Processing: %%c
      Echo Creating schedules on: %%c >>C:\MaintenanceRpt.txt
      SCHTASKS /Create /F /S %%c /SC WEEKLY /MO 2 /D !SDay! /ST !DCTime! /TN DiskCleanup /TR "C:\WINDOWS\SYSTEM32\cleanmgr.exe /sagerun" /RU "NT AUTHORITY\SYSTEM" >>C:\MaintenanceRpt.txt
      SCHTASKS /Create /F /S %%c /SC WEEKLY /MO 2 /D !SDay! /ST !DFTime! /TN DefragD /TR "C:\WINDOWS\SYSTEM32\defrag.exe D:" /RU "NT AUTHORITY\SYSTEM" >>C:\MaintenanceRpt.txt

Goto EndScript
Echo "C:\Computers.txt" file does not exist or file is empty!
:: Batch Script End

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now