Select Page

Today I got a query on My Youtube Channel on How to create a Powershell script to Disable AD User Account. There are multiple ways to do it lets see How we can Disable AD User Account with Powershell.

To disable Active Directory user account by user’s samAccountName and DistinguishedName, disable AD Users from specific OU, and disable Bulk AD users from CSV file using PowerShell script.

You can disable an AD account by using the Active Directory powershell cmdlet Disable-ADAccount.

Disable-ADAccount -Identity 

The Disable-ADAccount cmdlet disables an Active Directory user, computer, or service account. The Identityparameter specifies the Active Directory user, computer service account, or other service account that you want to disable. You can identify an account by its distinguished name (DN), GUID, security identifier (SID), or samAccountName.

Disable AD User Account with samAccountName

Import-Module ActiveDirectory
Disable-ADAccount -Identity JoyTest

Disable AD User Account with DistinguishedName

Import-Module ActiveDirectory
Disable-ADAccount -Identity "CN=JoyTest,OU=Delhi,DC=TestDomain,DC=local"

Disable Active Directory Users from Specific OU

Import-Module ActiveDirectory
Get-ADUser -Filter 'Name -like "*"' `
  -SearchBase "OU=Delhi,DC=TestDomain,DC=local" | Disable-ADAccount

Disable Bulk AD Users from CSV file using Powershell Script

  • Consider the CSV file Users.csv which contains set of Active Directory users to disable with the attribute samAccountName.
  • Copy the below Powershell script and paste in Notepad file.
  • Change the Users.csv file path with your own csv file path.
  • SaveAs the Notepad file with the extension .ps1 like Disable-Bulk-AD-Users-FromCSV.ps1 
Import-Module ActiveDirectory
Import-Csv "C:\Scripts\Users.csv" | ForEach-Object {
 $samAccountName = $_."samAccountName"
Get-ADUser -Identity $samAccountName | Disable-ADAccount
  • Now run the Disable-Bulk-AD-Users-FromCSV.ps1 file in Powershell to Disable Bulk Active Directory users from CSV file.
PS C:\Scripts>  .\Disable-Bulk-AD-Users-FromCSV.ps1

Note: This Script is in C:\Scripts Folder

This Above Command will Help and Soon I will work upon the Script and show you’re up in a Video How to Do Live in Front of you which will make things easy.

Anytime you have any questions just shoot the question and will be happy to assist you further on this hope this post of Powershell script to Disable AD User Account.