When it comes to defragmentation, the best recommendation I can make is to do something. The Windows scheduled task will be good enough for most situations, but for advanced configurations, you may want to leverage a more robust offering.

There is a ScheduledDefrag task that has its trigger inside set to disable by default. Is there a way through Powershell scripting to set that trigger to enable?

# Used by the RegisterTaskDefinition method
$TASK_CREATE_OR_UPDATE = 0x6

# Specify the computer name
$computerName = “localhost”
# Specify the task’s name
$taskName = “\Microsoft\Windows\Defrag\ScheduledDefrag”
$taskService = new-object -comobject “Schedule.Service”
$taskService.Connect($computerName)
$taskFolder = $taskService.GetFolder(“\”)
$registeredTask = $taskFolder.GetTask($taskName)
$registeredTask.Enabled = $TRUE
$taskDefinition = $registeredTask.Definition
$taskDefinition.Triggers | foreach-object { $_.Enabled = $TRUE }
[Void] $taskFolder.RegisterTaskDefinition($taskName, $taskDefinition,
$TASK_CREATE_OR_UPDATE, $NULL, $NULL, $taskDefinition.Principal.LogonType)

Other means:

If you don’t want to use the Windows scheduled task, the Disk Defragmenter tool, dfrgui.exe, is another way to set a defragmentation schedule. When you take this approach, the Windows scheduled task configuration (including the enabled/disabled trigger) will set to the new schedule

Other resources:

schtasks.exe – http://www.peetersonline.nl/index.php/powershell/managing-scheduled-tasks-remotely-using-powershell/ (PowerShell commands using schtasks.exe):

NB: you cannot use schtasks to modify the TRIGGER of the default defrag task. Prefer using powershell script with PowerShell Pack: http://archive.msdn.microsoft.com/PowerShellPack

To query the task : schtasks /query /s <computer> /TN \Microsoft\Windows\Defrag\ScheduledDefrag

To disable the task: schtasks /change /s <computer> /TN \Microsoft\Windows\Defrag\ScheduledDefrag /DISABLE

To update the execution time of that task : schtasks /change /s <computer> /TN \Microsoft\Windows\Defrag\ScheduledDefrag /ST 21:30 /ENABLE

To create a task using a XML file: schtasks /create /s <computer> /TN \Microsoft\Windows\Defrag\ScheduledDefrag /XML scheduledDefrag.xml

To delete a task (force): schtasks /delete /s <computer> /TN \Microsoft\Windows\Defrag\ScheduledDefrag /F

Other examples with schtasks: http://www.robvanderwoude.com/schtasks.php

Using PowerShell Pack: http://blog.powershell.no/2012/05/28/working-with-scheduled-tasks-from-windows-powershell/