Restore_Deleted_File.ps1

function Restore-DeletedFile {
    param (
        [Parameter(Mandatory = $true)]
        [string]$fileName,

        [Parameter(Mandatory = $true)]
        [string]$filePath
    )

    # Check if the file exists at the specified path
    if (-Not (Test-Path -Path $filePath)) {
        Write-Host "The specified file path doesn't exist."
        return
    }

    # Get the deleted objects from the AD Recycle Bin
    $deletedObjects = Get-ADObject -Filter 'isDeleted -eq $true' -IncludeDeletedObjects -ErrorAction Stop

    # Search for the file in the deleted objects
    $deletedFile = $deletedObjects | Where-Object { $_.SamAccountName -eq $fileName }

    # Check if the file was found in the deleted objects
    if (-Not $deletedFile) {
        Write-Host "The file '$fileName' was not found in the Active Directory Recycle Bin."
        return
    }

    # Restore the file from the AD Recycle Bin
    try {
        Restore-ADObject -Identity $deletedFile.DistinguishedName -ErrorAction Stop
        Write-Host "File '$fileName' successfully restored from the Active Directory Recycle Bin."
    } catch {
        Write-Host "Failed to restore the file '$fileName'. Error: $_"
    }
}