How To: Enable Defrag on Server 2008R2 and Windows 7 Using ConfigMgr Setttings Management

We found several systems recently where defrag was not enabled. Thanks to a little help from the TechNet forums, I was able to create a Compliance Settings Rule using PowerShell. Create a new compliance settings configuration item as follows:

      1. Choose a name (Such as Enable Defrag).
      2. Choose the supported platforms (all Windows 7, all Server 2008 R2)
      3. Create a new Setting, choose Script for the setting type, and Boolean for the data type.
      4. Paste the PowerShell code below for the Discovery Script:
        # 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 | % {$_.Enabled}
        
      5. Paste the PowerShell code below for the Remediation Script.
        $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)
        
      6. Create a new Compliance Rule tab of the configuration item, where Enabled = True, as shown next:
      7. Add this configuration item to a baseline and deploy to a collection. Note that in order to  execute Compliance Management scripts that use Powershell, you may need to modify the client setting named “PowerShell Execution Policy”  to Bypass.

Download EnableDefragOnServer08andWindows7.ps1.


About Greg Ramsey
Greg Ramsey is a Distinguished Engineer for Dell Digital - Services. He has a B.S. in Computer Sciences and Engineering from The Ohio State University and has co-authored many books over the years. Greg is also a board member of the Northwest System Center User Group and the Midwest Management Summit. ​Greg has been a Microsoft Endpoint Manager (ConfigMgr, Intune) MVP for over 14 years.

One Response to How To: Enable Defrag on Server 2008R2 and Windows 7 Using ConfigMgr Setttings Management

  1. George Simos says:

    Great article Greg! Just a note for the readers, please use the “Copy to Clipboard” icon on the source code sections instead of the usual Ctrl+C combination because this way you will get the clear source code without the line numbers! 😉

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: