Bulk Disable Active Directory Users with PowerShell
Managing user accounts in Active Directory can be a daunting task, especially when it comes to disabling multiple accounts that are no longer in use. In this post, we will share a PowerShell script that allows you to bulk disable users in Active Directory using a CSV file as input. This simple yet powerful script will help you maintain your user accounts more effectively.
At ServerEngine, we offer innovative solutions to streamline your server management processes. Visit [ServerEngine](https://serverengine.co) to learn more about our software and how it can help you optimize your IT operations.
### Step 1: Prepare Your CSV File
Create a CSV file containing the usernames or distinguished names (DN) of the users you wish to disable. This file should have a single column with the header `UserName`.
Example `users_to_disable.csv`:
“`
UserName
jdoe
jsmith
“`
### Step 2: Import the Required Module
Before we can disable users, we need to ensure the Active Directory module is imported.
“`powershell
Import-Module ActiveDirectory
### Step 3: Define the Disable Function
Next, we will create a function named `Disable-ADUsers` that reads the CSV file and disables each listed user account in Active Directory.
“`powershell
function Disable-ADUsers { param ( [string]$csvPath ) $users = Import-Csv -Path $csvPath foreach ($user in $users) { $userName = $user.UserName try { Disable-ADAccount -Identity $userName Write-Host "Successfully disabled user: $userName" } catch { Write-Host "Failed to disable user: $userName. Error: $_" } } }
### Step 4: Execute the Disable Function
Now, run the function with the path to your CSV file to disable the users.
“`powershell
Disable-ADUsers -csvPath "C:\Path\To\Your\users_to_disable.csv"
### Conclusion
This script provides a quick and efficient way to batch disable Active Directory user accounts. Just ensure that your CSV file is properly formatted and the path is correct. For more robust server management tools and resources, explore what [ServerEngine](https://serverengine.co) has to offer.