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

PowerShell: Microsoft SQL Administration

Script to Install Microsoft Clustering Service $proxy="http://proxy:8080"$exclusionList="localhost;*.kimconnect.com"function checkProxy{try{$connectionTest=iwr download.microsoft.com#$connectionSucceeds=Test-NetConnection -Computername download.microsoft.com -Port 443 -InformationLevel Quietif…

PowerShell: Set Virtual Machine Default Paths on Hyper-V Host of a Cluster

$newVirtualMachinePath='D:\VirtualMachines' $newVirtualHardDiskPath='D:\VirtualMachines' function getHyperVHostsInForest{ function includeRSAT{ $ErrorActionPreference='stop' [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 #$rsatWindows7x32='https://download.microsoft.com/download/4/F/7/4F71806A-1C56-4EF2-9B4F-9870C4CFD2EE/Windows6.1-KB958830-x86-RefreshPkg.msu' $rsatWindows7x64='https://download.microsoft.com/download/4/F/7/4F71806A-1C56-4EF2-9B4F-9870C4CFD2EE/Windows6.1-KB958830-x64-RefreshPkg.msu' $rsatWindows81='https://download.microsoft.com/download/1/8/E/18EA4843-C596-4542-9236-DE46F780806E/Windows8.1-KB2693643-x64.msu' $rsat1709 =…

PowerShell: How To Make A System App Do Nothing

# How-To-Make-Existing-System-App-Do-Nothing.ps1# Provide variables$hive="REGISTRY::HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WORDPAD.EXE"$key="(default)"$value="C:\Windows\dummy.exe"$defaultValue="C:\Program Files\Windows NT\Accessories\WORDPAD.EXE"# Dummy-File-Creator.ps1$dummyFile="C:\Windows\dummy.exe"$output = new-object byte[] 1; (new-object Random).NextBytes($output);[IO.File]::WriteAllBytes($dummyFile, $output);if…