Backup Active Directory

This script creates a backup of your Active Directory by exporting all users, groups, and OUs to CSV files. This can be useful for restoring data or for reference in case of accidental deletions.

# Define output directory for backups
$backupDir = "C:\Backups\AD_Backup"
if (!(Test-Path -Path $backupDir)) { New-Item -ItemType Directory -Path $backupDir }

# Backup all users
Get-ADUser -Filter * -Properties * | Select-Object DisplayName, SamAccountName, EmailAddress, Department, Title |
    Export-Csv -Path "$backupDir\AD_Users.csv" -NoTypeInformation -Encoding UTF8

# Backup all groups
Get-ADGroup -Filter * -Properties * | Select-Object Name, GroupCategory, GroupScope, SamAccountName |
    Export-Csv -Path "$backupDir\AD_Groups.csv" -NoTypeInformation -Encoding UTF8

# Backup all OUs
Get-ADOrganizationalUnit -Filter * -Properties * | Select-Object Name, DistinguishedName |
    Export-Csv -Path "$backupDir\AD_OUs.csv" -NoTypeInformation -Encoding UTF8

Write-Output "Active Directory backup completed in $backupDir"

• Explanation: This script exports details of users, groups, and OUs to separate CSV files in a specified backup directory.

Last updated