Restore AD Objects from CSV Backup

This script restores user accounts from a CSV backup file if accounts were deleted or modified unintentionally.

# Path to the CSV backup file with user data
$backupFile = "C:\Backups\AD_Backup\AD_Users.csv"

# Import user data from CSV and create users
$users = Import-Csv -Path $backupFile

foreach ($user in $users) {
    # Check if user already exists
    if (!(Get-ADUser -Filter {SamAccountName -eq $user.SamAccountName})) {
        # Create the user
        New-ADUser -Name $user.DisplayName -GivenName $user.GivenName -Surname $user.Surname -SamAccountName $user.SamAccountName `
                   -UserPrincipalName "$($user.SamAccountName)@yourdomain.com" -Enabled $true -Department $user.Department -Title $user.Title
        Write-Output "Restored user account: $($user.DisplayName)"
    } else {
        Write-Output "User $($user.DisplayName) already exists, skipping."
    }
}

• Explanation: This script reads user data from a CSV backup file and recreates accounts if they don’t exist in Active Directory.

Last updated