How to Schedule a Weekly Defrag on a Storage Spaces Mirror in Windows 11
🔍 WiseChecker

How to Schedule a Weekly Defrag on a Storage Spaces Mirror in Windows 11

You want to keep your Storage Spaces mirror volume running fast, but Windows 11 does not automatically defrag Storage Spaces drives with the same schedule as standard disks. Storage Spaces is a software-based RAID system that stripes and mirrors data across multiple physical drives, and its virtual nature requires a different approach for optimization. This article explains how to schedule a weekly defrag task specifically for a Storage Spaces mirror using the built-in Defragment and Optimize Drives tool and Task Scheduler. By the end, you will have a reliable, automatic weekly defrag routine that maintains peak performance for your mirrored storage.

Key Takeaways: Scheduling Weekly Defrag for Storage Spaces Mirror

  • Storage Spaces virtual disk: Defragmentation works on the virtual volume, not individual physical drives, and requires the virtual disk to be visible in Optimize Drives.
  • Optimize Drives tool: Use the built-in Windows tool to manually defrag or set a custom schedule per drive, but it does not allow weekly scheduling for Storage Spaces by default.
  • Task Scheduler with PowerShell: Create a weekly task that runs the Optimize-Volume PowerShell cmdlet on the Storage Spaces virtual disk to bypass the default monthly limitation.

ADVERTISEMENT

Understanding Storage Spaces Mirror and Defragmentation

Storage Spaces is a Windows feature that pools physical disks into a storage pool and creates virtual drives from that pool. A mirror layout duplicates data across two or more drives to provide fault tolerance. When you create a Storage Spaces mirror, Windows presents the virtual disk as a single volume to the operating system. Defragmentation reorganizes fragmented files on this virtual volume to improve read and write performance. However, the Optimize Drives tool in Windows 11 applies a default monthly optimization schedule to Storage Spaces volumes, even though standard SSDs and HDDs may have different default schedules. For a Storage Spaces mirror that experiences heavy write activity, monthly optimization may not be enough to prevent performance degradation. Weekly defragmentation ensures that file system fragmentation stays low, especially for volumes used for databases, virtual machines, or large file archives. The key prerequisite is that the Storage Spaces virtual disk must be healthy, online, and have a drive letter assigned before you can target it with defrag commands.

Steps to Schedule a Weekly Defrag for a Storage Spaces Mirror

Follow these steps to create a scheduled task that defrags your Storage Spaces mirror every week using PowerShell and Task Scheduler. You need administrative privileges on the Windows 11 computer.

Step 1: Identify the Storage Spaces Virtual Disk

  1. Open PowerShell as Administrator
    Press the Windows key, type PowerShell, right-click Windows PowerShell or Terminal, and select Run as administrator. Click Yes in the User Account Control prompt.
  2. List all volumes
    Run the command Get-Volume | Where-Object {$_.DriveType -eq 'Fixed'}. This shows all fixed drives including the Storage Spaces virtual disk. Note the DriveLetter column value for your Storage Spaces mirror, for example E:.
  3. Verify the Storage Spaces virtual disk
    Run Get-VirtualDisk | Format-List FriendlyName, OperationalStatus, HealthStatus to confirm the virtual disk name. For example, MyMirror. The virtual disk must show OperationalStatus: OK and HealthStatus: Healthy.

Step 2: Create a PowerShell Script for the Defrag Task

  1. Open Notepad
    Press the Windows key, type Notepad, and press Enter.
  2. Write the defrag script
    Copy and paste the following lines into Notepad. Replace E: with your actual Storage Spaces drive letter. This script uses Optimize-Volume with the -ReTrim and -Defrag parameters. For a mirror volume that uses thin provisioning, -ReTrim reclaims unused space. For a thick-provisioned mirror, you can omit -ReTrim.

    # Defrag Storage Spaces mirror volume E:
    Optimize-Volume -DriveLetter E -Defrag -ReTrim -Verbose

    If you want to perform only a defrag without re-trim, use:

    Optimize-Volume -DriveLetter E -Defrag -Verbose

  3. Save the script
    Click File > Save As. In the Save as type dropdown, select All Files (). Name the file DefragMirror.ps1 and save it to a folder like C:\Scripts. Create the folder if it does not exist.

Step 3: Configure Task Scheduler to Run the Script Weekly

  1. Open Task Scheduler
    Press the Windows key, type Task Scheduler, and press Enter.
  2. Create a new task
    In the right-hand Actions pane, click Create Task.
  3. Set the General tab
    In the Name field, type Weekly Defrag Storage Spaces Mirror. Check Run whether user is logged on or not. Check Run with highest privileges. In the Configure for dropdown, select Windows 11 or Windows 10.
  4. Set the Triggers tab
    Click New. In the Begin the task dropdown, select On a schedule. Set Weekly. Choose the day and time you want the defrag to run, for example Sunday at 2:00 AM. Ensure the task is enabled. Click OK.
  5. Set the Actions tab
    Click New. In the Action dropdown, select Start a program. In the Program/script field, type powershell.exe. In the Add arguments (optional) field, type:

    -ExecutionPolicy Bypass -File "C:\Scripts\DefragMirror.ps1"

    Click OK.

  6. Set Conditions and Settings tabs
    On the Conditions tab, uncheck Start the task only if the computer is on AC power if you want the task to run even on battery. On the Settings tab, check Allow task to be run on demand and If the running task does not end when requested, force it to stop. Click OK.
  7. Enter your credentials
    Enter your Windows account password when prompted, then click OK.

Step 4: Test the Scheduled Task

  1. Run the task manually
    In Task Scheduler, right-click the Weekly Defrag Storage Spaces Mirror task and select Run.
  2. Check the result
    Open PowerShell as Administrator and run Get-Volume -DriveLetter E | Format-List Size, SizeRemaining, DefragAnalysis. If the defrag succeeded, you will see the last optimization date. You can also check the History tab of the task in Task Scheduler for success or failure events.

ADVERTISEMENT

Common Issues and Limitations with Weekly Defrag on Storage Spaces

The Optimize-Volume cmdlet fails with “Access Denied” on a Storage Spaces virtual disk

This error occurs when the PowerShell script does not run with administrator privileges. Ensure the Task Scheduler task has the Run with highest privileges option checked. Also verify that the account running the task has local administrator rights on the Windows 11 computer.

Weekly defrag does not improve performance on a Storage Spaces mirror used for archival data

If the mirror volume contains mostly static files that are rarely modified, fragmentation remains low and weekly defragmentation provides no benefit. For such volumes, you can change the schedule to monthly or disable the task. Use the Optimize-Volume -DriveLetter E -Analyze -Verbose command to check fragmentation percentage before scheduling.

The Task Scheduler task fails because the Storage Spaces virtual disk is not available at boot time

Storage Spaces virtual disks may take a few seconds to appear after Windows starts. If your scheduled task runs at system startup, it may fail. Set the task to run at a specific time, such as 2:00 AM, when the system is already running, or add a delay of 5 minutes in the trigger settings.

Optimize Drives Schedule vs Task Scheduler for Storage Spaces Mirror

Item Optimize Drives Tool Task Scheduler with PowerShell
Schedule granularity Monthly only for Storage Spaces volumes Any interval: weekly, daily, hourly, custom
Ease of setup Simple GUI, no scripting required Requires creating a PowerShell script and task
Defrag parameters Uses default Windows optimization algorithm Full control: -Defrag, -ReTrim, -SlabConsolidate
Error handling Limited to event log entries Can add logging and retry logic in the script
Run on battery Respects Windows power settings Configurable in task conditions

You can now maintain a weekly defrag schedule for your Storage Spaces mirror using Task Scheduler and PowerShell. This approach gives you full control over the optimization frequency and parameters, ensuring your mirrored volume stays fast even under heavy write workloads. To further improve performance, consider enabling write-back caching on the Storage Spaces mirror if your physical drives support it. For critical data, always verify the health of your storage pool and virtual disk before running any defragmentation.

ADVERTISEMENT