Use PowerShell to Start Domain Controller Garbage Collection

Garbage Collection runs every 12 hours on a domain controller. So, what is it, and, more importantly, how can we kick it off with PowerShell?

What is it?
Garbage collection identifies tombstones and recycled-objects that have been kept for at least the tombstone lifetime and then removes them. On a happy DC, there should not be a need to manually trigger garbage collection.

How can we start Garbage Collection with PowerShell?
There are times you may want to start garbage collection manually. You might do it as part of a trouble-shooting exercise or in preparation for some lingering object clean-up work… whatever the reason, here’s the juicy stuff!

   Juicy Stuff

Hmmmm.... juicy. Now, specify the domain controller that you want the process to start on:

$DomainController = "NINJADC01"

  

Now, get the domain’s SID:

$DomainSid = (Get-ADDomain).DomainSid.Value

 
Next, connect to the RootDSE. We need to expose methods not available with the Get-ADRootDSE cmdlet, so we use the [ADSI] type accelerator to create a System.DirectoryServices.DirectoryEntry object::

$RootDSE = [ADSI]"LDAP://$DomainController/RootDSE"

 

Now, put the update into memory:

$RootDSE.Put("doGarbageCollection", $DomainSid)

  
Finally, write the update back to Active Directory:

$RootDSE.SetInfo()

 
Together it looks like this.

$DomainController = "NINJADC01"

$DomainSid = (Get-ADDomain).DomainSid.Value

$RootDSE = [ADSI]"LDAP://$DomainController/RootDSE"

$RootDSE.Put("doGarbageCollection", $DomainSid)

$RootDSE.SetInfo()

 

Once Garbage Collection is kicked off, you’ll see event 700, in the Directory Service log, telling you an online defragmentation has begun.

 
When the full pass on the AD database has finished you’ll see event 701 telling you the online defragmentation has finished.

 

 Ask DS: Taking out the Trash