Disabling Active Directory Users with PowerShell (Generalized)
A guide to efficiently disabling user accounts in specific Organizational Units (OUs) using PowerShell.
Introduction
This document provides a step-by-step guide on how to disable Active Directory (AD) user accounts located within specific Organizational Units (OUs) using PowerShell. This method is non-destructive, meaning the accounts are disabled and not deleted, allowing for easy re-enablement if needed.
We will focus on disabling all users within a 'YourTopLevelOU' and a 'YourNestedOU', assuming 'YourNestedOU' is nested under 'YourParentOU' and 'YourTopLevelOU' is a top-level OU directly under your domain (e.g., 'contoso.com').
Prerequisites: Ensure you are running PowerShell with administrative privileges on a Domain Controller or a machine with the Remote Server Administration Tools (RSAT) for Active Directory installed.
1. Understanding Distinguished Names (DNs)
To target specific OUs, you need their precise Distinguished Name (DN). The DN is a unique identifier that specifies the exact location of an object within the Active Directory hierarchy.
- Your Domain:
yourdomain.com
(e.g.,contoso.com
) (translates toDC=yourdomain,DC=com
orDC=contoso,DC=com
) - YourTopLevelOU: This OU is directly under your domain.
DN:OU=YourTopLevelOU,DC=yourdomain,DC=com
- YourParentOU: This OU is also directly under your domain.
DN:OU=YourParentOU,DC=yourdomain,DC=com
- YourNestedOU: This OU is nested inside the 'YourParentOU'.
DN:OU=YourNestedOU,OU=YourParentOU,DC=yourdomain,DC=com
Tip: Verifying DNs: To get the exact DN for any object in Active Directory Users and Computers (ADUC), enable "Advanced Features" under the "View" menu. Then, right-click the object, go to "Properties," click the "Attribute Editor" tab, and find the distinguishedName
attribute. Copy its value directly.