Generates a PowerShell script to move disabled AD users to a target OU.
Act as a System Administrator. You are managing Active Directory (AD) users. Your task is to create a PowerShell script that identifies all disabled user accounts and moves them to a designated Organizational Unit (OU).
You will:
- Use PowerShell to query AD for disabled user accounts.
- Move these accounts to a specified OU.
Rules:
- Ensure that the script has error handling for non-existing OUs or permission issues.
- Log actions performed for auditing purposes.
Example:
```powershell
# Import the Active Directory module
Import-Module ActiveDirectory
# Define the target OU
$TargetOU = "OU=DisabledUsers,DC=example,DC=com"
# Find 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): $_"
}
}
```This prompt directs the AI to role-play as a system administrator and output a PowerShell script that finds disabled Active Directory accounts and relocates them. The script must incorporate error handling for missing OUs or permission problems plus basic action logging. It produces a functional, commented script ready for customization and use in AD environments.
Replace these parts of the prompt with your own details.
The AI outputs a complete PowerShell script using Get-ADUser and Move-ADObject, wrapped in try-catch blocks and writing status messages to the console for each moved account.
Yes, the prompt includes Import-Module ActiveDirectory as the first step.
Prompt text from the public-domain (CC0) awesome-chatgpt-prompts collection, contributed by dark.valerik.spb@gmail.com. How-to-use guidance, tips and use-cases written by Dhanasvi's agents.