PowerShell Script to Move Disabled AD Users to Specific OU
Contributed by dark.valerik.spb@gmail.com
Improved by Laravel Company · 2026-09-07
Improved prompt:
Act as an experienced PowerShell and Active Directory (AD) administrator. You are responsible for managing user accounts within our organization's AD forest. Your task is to write a sophisticated PowerShell script that performs the following critical functions:
- Identify and enumerate all disabled user accounts currently residing in the Active Directory.
- Implement a robust error-handling mechanism to ensure that the script gracefully manages any exceptions that may occur during the execution process.
- Move each identified disabled user account to a designated Organizational Unit (OU) specified by the script variable
${targetOU}.
Key requirements and constraints:
- The script must be designed for optimal performance, minimizing unnecessary AD queries and reducing the overall execution time.
- The target OU is defined by the variable
${targetOU}, which should be set to the distinguished name of the Organizational Unit where disabled users will be relocated. - Ensure that the script includes comprehensive comments explaining each significant step, allowing other administrators to understand and modify the script as needed.
- The script should not modify any attributes of the user accounts other than their OU location.
Example PowerShell script template:
# Define the target OU where disabled users will be moved
$targetOU = "OU=DisabledUsers,DC=yourdomain,DC=com"
# Initialize an empty array to store any errors that occur during execution
$errors = @()
try {
# Get all disabled user accounts in the Active Directory
$disabledUsers = Get-ADUser -Filter {Enabled -eq $false} -ErrorAction Stop
# Iterate through each disabled user account
foreach ($user in $disabledUsers) {
try {
# Attempt to move the user account to the target OU
Move-ADObject -Identity $user.DistinguishedName -TargetPath $targetOU -ErrorAction Stop
# Log the successful movement of the user account
Write-Host "Moved: $($user.SamAccountName) to $targetOU"
}
catch {
# Capture the error that occurred during the movement attempt
$error = $_
$errors += "$($error.Message) - User: $($user.SamAccountName)"
}
}
}
catch {
# Capture the error that occurred during the initial user account retrieval
$errors += "Error retrieving disabled users: $_"
}
# Log any errors that occurred during script execution
if ($errors) {
Write-Host "Errors occurred during script execution:"
foreach ($error in $errors) {
Write-Host $error
}
}Please ensure that the script adheres to the best practices for PowerShell and Active Directory administration. Keep the script as efficient as possible while maintaining clarity and readability. The script should be ready for deployment in our production environment.
Expected output format:
The script should log the successful movement of each user account to the specified target OU. If any errors occur during the execution, they should be captured and logged for further investigation.
Remember to test the script on a non-production environment before deploying it in our active directory infrastructure.
Original prompt (before our improvements)
Act as a System Administrator. You are tasked with managing user accounts in Active Directory (AD). Your task is to create a PowerShell script that: - Identifies all disabled user accounts in the AD. - Moves these accounts to a designated Organizational Unit (OU) specified by the variable ${targetOU}. Rules: - Ensure that the script is efficient and handles errors gracefully. - Include comments in the script to explain each section. Example PowerShell Script: ``` # Define the target OU $targetOU = "OU=DisabledUsers,DC=yourdomain,DC=com" # Get all disabled user accounts $disabledUsers = Get-ADUser -Filter {Enabled -eq $false} # Move each disabled user to the target OU foreach ($user in $disabledUsers) { try { Move-ADObject -Identity $user.DistinguishedName -TargetPath $targetOU Write-Host "Moved: $($user.SamAccountName) to $targetOU" } catch { Write-Host "Failed to move $($user.SamAccountName): $_" } } ``` Variables: - ${targetOU} - The distinguished name of the target Organizational Unit where disabled users will be moved.