Active Directory - Compact Directory Database

Every 12 hours Active Directory performs garbage collections where it defragments whitespace within the database. This whitespace is optimized for performance but it is not returned to the file system.
Garbage Collection Increase the garbage collection logging level from 0 to 1 in order to determine how much whitespace exists in a database. This change in logging level will result in a Event ID 1646 being logged to the Directory Service log. The event will show how much total space is used by the database file and how much recoverable whitespace exists. Configure Garbage Collection Logging View current setting:
$Reg = "HKLM:\SYSTEM\CurrentControlSet\Services\NTDS\Diagnostics"
Get-ItemProperty -Path $Reg
Set logging level:
$Reg = "HKLM:\SYSTEM\CurrentControlSet\Services\NTDS\Diagnostics"
Set-ItemProperty -Path $Reg -Name "6 Garbage Collection" -Type DWORD -Value 1
Compact (Offline Defragmentation) Prepare for Compacting: Create a folder named "compact" in D:\NTDS. Open an elevated PowerShell prompt Stop the AD DS Service by typing "stop-service ntds -force" Begin Compacting: Enter the command "ntdsutil" At the ntdsutil prompt, type "activate instance ntds" and press "ENTER" Type "files" and press "ENTER" To begin compacting the database type compact to "d:\NTDS\compact" If compacting completed with errors perform an integrity check (See Perform Integrity Check bellow) Finish Compacting: Rename "D:\NTDS\ndts.dit" "D:\NTDS\ndts.dit.bk" so that it isn't overwritten Copy the compacted database file from "D:\NTDS\compact" to "D:\NTDS\compact" by typing "copy D:\NTDS\compact\ndts.dit D:\NTDS\ndts.dit" Delete all existing log files in "L:\Logs" by typing "del L:\Logs\*.logs" Perform Integrity Check: Enter the command "ntdsutil" At the ntdsutil prompt, type "activate instance ntds" and press "ENTER" Type "files" and press "ENTER" To begin the integrity check, type integrity Database Integrity Check If there are any errors from the integrity check, delete the compacted database file, "D:\NTDS\ntds.dit", and perform the copy again Restart AD DS: Start the AD DS Service by typing "start-service ntds") If successful Event IDs 1000 and 1394 should appear in the "Directory Service" log If Event IDs 1046 and 1168 appear in the "Directory Service" log check the database integrity again If the database integrity fails again: - Stop the AD DS service ("stop-service ntds -force") - Delete the compacted database file ("del D:\NTDS\ntds.dit") - Rename the original database file ("D:\NTDS\ntds.dit.bk" to "D:\NTDS\ntds.dit") - Compact the database - Rename the original database and copy the compacted database - Perform integrity check If the integrity check succeeds but errors persist when starting the AD DS service, perform a "semantic database analysis with fixup