Defrag All Harddrives

under Windows PowerShell

 

The version of defrag that comes with most recent installs of Windows does not allow for scheduling…to get around this limitation, the following VBScript will enumerate all fixed harddrives on a machine, and launch defrag via command like for that drive. Set it up as a scheduled task and forget about it.

'* Copyright (C) 2004 Andrew Loree
'* $Id: defrag.vbs,v 1.2 2004/06/04 11:59:39 andy Exp $
'****************************************************************************
'* defrag.vbs - Enumerates all fixed hard drives on
'* running 'degrag {drive_letter} -f' to defrag the drives
'* from a commandline.  Create as a scheduled task
'****************************************************************************
'*  This program is free software; you can redistribute it and/or
'*  modify it under the terms of the GNU General Public License
'*  as published by the Free Software Foundation; either version 2
'*  of the License, or (at your option) any later version.
'*
'*  This program is distributed in the hope that it will be useful,
'*  but WITHOUT ANY WARRANTY; without even the implied warranty of
'*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'*  GNU General Public License for more details.
'*
'*  You should have received a copy of the GNU General Public License
'*  along with this program; if not, write to the Free Software
'*  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
'****************************************************************************
'* Version History:
'*	1.0 - Initial Release
'*	1.1 - Added a trailing colon to the drive letter
'****************************************************************************
Option Explicit
Dim oShell, oFS, oDrive, nResults
	Set oShell = CreateObject("WScript.Shell")
	Set oFS = CreateObject("Scripting.FileSystemObject")
	For Each oDrive In oFS.Drives
		If (oDrive.DriveType = 2) Then
			nResults = oShell.Run("defrag " & oDrive.DriveLetter & ": -f", 1, TRUE)
		End If
	Next
	Set oShell = Nothing