# Remove Protect Object setting from Organizational Unit via PowerShell

Use PowerShell to find OUs protected from accidental deletion and remove that flag in bulk when you need to move or clean them up carefully.

blog

13 Dec 2017 en

[View images (2)](#pf-content-image-1-0)

Sometimes an **Organizational Unit** refuses to move or delete even when you are a Domain Admin, which makes the error feel misleading at first.

[!\[Active Directory Domain Services error showing that the Users-Test-Accounts organizational unit cannot be moved because access is denied.\](/wp-content/uploads/2017/12/img_5a31774a07b2c.png)](/wp-content/uploads/2017/12/img_5a31774a07b2c.png)

In many cases the reason is simply the **Protect object from accidental deletion** flag.

[!\[Organizational Unit properties in Active Directory with Protect object from accidental deletion enabled on the Object tab.\](/wp-content/uploads/2017/12/img_5a31776fdb918.png)](/wp-content/uploads/2017/12/img_5a31776fdb918.png)

If you only have one OU to deal with, the GUI is fine. If you have a whole branch of legacy OUs to clean up, PowerShell is much faster and much safer because you can review the target list first.

## Find protected OUs under a specific branch

Start by listing only the OUs below the part of the tree you care about:

```powershell
Import-Module activedirectory

# Path to search in for OUs
$searchbase = "OU=Users,OU=Accounts,OU=Production,DC=test,DC=pl"

# Get all OUs that are protected
$protectedOrganizationalUnits = Get-ADOrganizationalUnit -SearchBase $searchbase -Filter * -Properties ProtectedFromAccidentalDeletion |
    Where-Object { $_.ProtectedFromAccidentalDeletion -eq $true }

# Display OUs that are protected
$protectedOrganizationalUnits | Select DistinguishedName, ProtectedFromAccidentalDeletion, Name

# Disable protection
#$protectedOrganizationalUnits | Set-ADOrganizationalUnit -ProtectedFromAccidentalDeletion $false
```

## Why the last line is commented out

That last line is intentionally disabled in the sample. I strongly prefer this pattern for bulk Active Directory changes:

- discover the objects first
- review the result set
- only then enable the write action

It is very easy to target more of the directory tree than you intended if the `SearchBase` is too broad.

## Safer workflow

Before uncommenting the change, consider exporting the result:

```powershell
$protectedOrganizationalUnits |
    Select-Object DistinguishedName, Name |
    Export-Csv .\ProtectedOUs.csv -NoTypeInformation
```

That gives you a quick review file before you change anything.

## Current note

The `Get-ADOrganizationalUnit` and `Set-ADOrganizationalUnit` cmdlets are still documented by Microsoft for current Windows Server PowerShell modules, so this remains a valid way to manage the `ProtectedFromAccidentalDeletion` setting in bulk.

## Related Projects

Projects that align with this post's tags, categories, and content.

[All projects](/projects/)

Matching projects...

[Back to Blog](/blog)

## Related posts

- [Azure ADConnect Export Failed – Permission-issue error](/blog/azure-adconnect-export-failed-permission-issue-error/)
- [Delegating Active Directory attribute physicalDeliveryOfficeName](/blog/delegating-active-directory-attribute-physicaldeliveryofficename/)
- [Mastering Active Directory Hygiene: Automating Stale Computer Cleanup with CleanupMonster](/blog/mastering-active-directory-hygiene-automating-stale-computer-cleanup-with-cleanupmonster/)

<!-- PowerForge markdown artifact sha256:9B7C824385ED4182737342300B50E3A5AA888F99E080CA08F312B36F2FEABF75 -->
