Posted On June 27, 2019

PowerShell: Recover Deleted Active Directory Objects

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: Recover Deleted Active Directory Objects
$domain="kimconnect"
$ltd="com"
$dc="dc01"
$userToRecover="Tom Cruise"

# Enable Active Directory Recycle Bin
# Enable-ADOptionalFeature 'Recycle Bin Feature' -Scope ForestOrConfigurationSet -Target "$domain.$ltd" -Confirm:$False
Enable-ADOptionalFeature "Recycle Bin Feature" -server ((Get-ADForest -Current LocalComputer).DomainNamingMaster) -Scope ForestOrConfigurationSet -Target (Get-ADForest -Current LocalComputer) -Confirm:$False

# Find deleted objects
Get-ADObject -filter 'isdeleted -eq $true -and name -ne "Deleted Objects"' -includeDeletedObjects -property *
Get-ADObject -ldapFilter:"(msDS-LastKnownRDN=*)" –IncludeDeletedObjects

# Restore user
Get-ADObject -Filter {displayName -like $userToRecover} -IncludeDeletedObjects | Restore-ADObject

# Restore using ntdsutil
$dnPath="CN=Steve Ardis,ou=Users,dc=dc,dc=local"
ntdsutil "authoritative restore" "restore object $dnPath" q q

# Restore using PowerShell
Get-ADObject -Filter 'samaccountname -eq $userToRecover' -IncludeDeletedObjects | Restore-ADObject

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

Hyper-V: Cloning a Virtual Machine

Update 11/25/2021: There's anther variation of this function at: https://blog.kimconnect.com/powershell-create-hyper-v-guest-vm-from-virtual-disk-vhdx/This function will dynamically detect source…

PowerShell: CredSSP

# Part 1: enable client mode on the local jump box $remoteComputer='SHERVER009' Enable-WSManCredSSP Client -DelegateComputer…

Python: Package Installer for Python (PIP)

This comes preinstalled with Python 3.4 or higher. Similar to the Microsoft PowerShell Gallery, Python.org…