I will share with you some scripts to answer your query :
1. Release "Object from Accidental Deletion" Protection for Disabled Users in an OU:
# Specify the DN (DistinguishedName) of the source OU with disabled users<br> $sourceOU = "OU=DisabledUsers,DC=example,DC=com"<br> <br> # Get a list of disabled user objects in the source OU<br> $disabledUsers = Get-ADUser -SearchBase $sourceOU -Filter {Enabled -eq $false}<br> <br> # Iterate through the list and remove the "Object from Accidental Deletion" protection<br> foreach ($user in $disabledUsers) {<br> $user | Set-ADObject -ProtectedFromAccidentalDeletion $false<br> } |
---|
Replace "OU=DisabledUsers,DC=example,DC=com"
with the DN of your actual source OU.
2. Move Disabled Users to a Special OU:
# Specify the DN of the target special OU where you want to move the disabled users<br> $targetOU = "OU=SpecialUsers,DC=example,DC=com"<br> <br> # Move disabled users to the special OU<br> $disabledUsers | ForEach-Object {<br> Move-ADObject -Identity $_.DistinguishedName -TargetPath $targetOU<br> } |
---|
Replace "OU=SpecialUsers,DC=example,DC=com"
with the DN of your actual target OU.
3. Reactivate Disabled Users in Certain OUs:
If you want to reactivate users in specific OUs, you'll need to identify those OUs and enable the users within them. Here's a basic example:
# Specify the DNs of the OUs where you want to reactivate users<br> $ou1 = "OU=OU1,DC=example,DC=com"<br> $ou2 = "OU=OU2,DC=example,DC=com"<br> <br> # Get a list of disabled users in the specified OUs<br> $usersToReactivate = Get-ADUser -SearchBase $ou1, $ou2 -Filter {Enabled -eq $false}<br> <br> # Iterate through the list and enable the users<br> foreach ($user in $usersToReactivate) {<br> $user | Enable-ADAccount<br> } |
---|
Replace $ou1 and $ou2 with the DNs of the OUs where you want to reactivate users.
Before running these scripts in a production environment, it's crucial to thoroughly test them in a safe environment or with a limited set of users to ensure they behave as expected.