Schedule a defragment and system cleanup on a remote machine

Posted on 2007-08-09
Last Modified: 2011-09-20

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.

Question by:bsharath
    1 Comment
    LVL 26

    Accepted Solution

    :: ================
    :: ================
    :: * 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)
    SET SDay=FRI
    :: 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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Have you ever had a hard drive that you can't boot into, but need to change the registry? Here is the solution! This article guides you through accessing and editing a registry of a non-primary drive. To read registry information on a non-prim…
    Welcome to my series of short tips on migrations. Whilst based on Microsoft migrations the same principles can be applied to any type of migration. My first tip is around source server preparation. No migration is an easy migration, there is a…
    This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
    In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now