Automate defragmentation using PowerShell script

What is Defragmentation:

Disk defragmentation is a process used in computer storage to improve the performance and efficiency of a hard disk drive (HDD) or solid-state drive (SSD).

Fragmentation:

When you save, modify, or delete files on your computer's hard drive, the data can become fragmented over time

Disk defragmentation:

It is the process of rearranging the fragmented data on a hard drive so that related pieces of a file are stored together, in contiguous blocks. This optimization helps improve disk access times and overall system performance.

How it is Works:

When you initiate a defragmentation process (either manually or through an automated schedule), the defragmentation utility in the operating system scans the entire disk, identifies fragmented files, and then moves the file fragments to adjacent or contiguous blocks on the disk. The goal is to reduce the amount of head movement required to access data, which can result in faster read and write speeds.

I am using the below PowerShell (ps1) script  to automate the defragmentation,

Step 1: Open the Notepad.

Step 2: Copy and paste the below script.

Step 3: Save as the script in the name of defrag.ps1 .

 

# Get the system name
$SystemName = [System.Environment]::MachineName

# Define the path where you want to save the combined output log file
$CombinedOutputLogFileName = "OptimizeOutput_${SystemName}_C_and_D.log"
$CombinedOutputLogFilePath = [System.IO.Path]::Combine([System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::MyDocuments), $CombinedOutputLogFileName)

# Start capturing the verbose output to the combined log file
Start-Transcript -Path $CombinedOutputLogFilePath -Append

# List of drive letters to optimize
$DriveLetters = @("C", "D")

foreach ($DriveLetter in $DriveLetters) {
   # Display a message indicating which drive is being optimized
   Write-Host "Optimizing drive $DriveLetter..."

   # Run the Optimize-Volume command with verbose output
   Optimize-Volume -DriveLetter $DriveLetter -Defrag -Verbose

   # Display a message indicating the completion of optimization for the drive
   Write-Host "Optimization for drive $DriveLetter completed."
}

# Stop capturing the verbose output
Stop-Transcript

# Display a message indicating where the combined output log has been saved
Write-Host "Combined Optimize-Volume output log saved to $CombinedOutputLogFilePath"
 

For the above script optimize and combine the output and it's stored in the document path.

Step 3: Open the task scheduler.

Step 4: Right-click and create a task.

Step 5: Next, select the timing (we migrating quarterly) from the trigger tab.

Step 6: Apply, and then OK. The trigger was started. 

Remarks:

When finished, the output is saved in the C:\Users\Documents\OptimizeOutput_$Systemname_C_and_D.txt

Note:

Defrag SSD is more like a re-write process to the disk, it could shorten the life span of SSD. Disk defragmentation is only recommended for HDDs. Defrag SSD is more like a re-write process to the disk, it could shorten the life span of SSD. 

Follow Us On

Registered Office

CHG IT CONSULTANCY PVT LTD

STPI Technology Incubation Centre,
2nd Floor, No.5, Rajiv Gandhi Salai,
Taramani, Chennai – 600113,
Tamil Nadu, INDIA

Parent Office

CIC Corporation

2-16-4 Dogenzaka, Shibuya-ku,
Nomura Real Estate,
Shibuya Dogenzaka Building,
Tokyo 150-0043, JAPAN

  +81 03-3496-1571
AboutUs

CHG IT Consultancy Pvt. Ltd. is a subsidiary of CIC Holdings Co. Ltd. Japan. Our company is focused on IT related solutions to reap the benefits of global popularity of Software Industry.

Registered Office
CHG IT CONSULTANCY PVT LTD

STPI Technology Incubation Centre,
2nd Floor, No.5, Rajiv Gandhi Salai,
Taramani, Chennai – 600113,
Tamil Nadu, INDIA

CIC Corporation

2-16-4 Dogenzaka, Shibuya-ku,
Nomura Real Estate,
Shibuya Dogenzaka Building,
Tokyo 150-0043, JAPAN

+81 03-3496-1571