Enable AD User Account using Powershell Script

In this article, I am going give powershell script examples to Enable Active Directory user account by user’s samAccountName and DistinguishedName, Enable AD Users from specific OU, and Enable Bulk AD users from CSV file using powershell script.

You can Enable an AD Account by using the Active Directory powershell cmdlet Enable-ADAccount.

Enable-ADAccount -Identity <adaccount>

The Enable-ADAccount cmdlet enables an Active Directory user, computer, or service account. The Identity parameter specifies the Active Directory user, computer service account, or other service account which you want to enable. You can identify an account by its distinguished name (DN), GUID, security identifier (SID), or samAccountName.

Summary:

Enable AD User Account with samAccountName

Import-Module ActiveDirectory
Enable-ADAccount -Identity MorganTest

Enable AD User Account with DistinguishedName

Import-Module ActiveDirectory
Enable-ADAccount -Identity "CN=MorganTest,OU=London,DC=TestDomain,DC=local"

Enable Active Directory Users from Specific OU

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

Enable Bulk AD Users from CSV file using Powershell Script

   1. Consider the CSV file Users.csv which contains set of Active Directory users to enable with the attribute samAccountName.

Enable Active Directory User Account using Powershell Script

   2. Copy the below Powershell script and paste in Notepad file.
   3. Change the Users.csv file path with your own csv file path.
   4. SaveAs the Notepad file with the extension .ps1 like Enable-Bulk-AD-Users-FromCSV.ps1

Powershell script file: Download Enable-Bulk-AD-Users-FromCSV.ps1

Import-Module ActiveDirectory
Import-Csv "C:\ScriptsUsers.csv" | ForEach-Object {
 $samAccountName = $_."samAccountName" 
Get-ADUser -Identity $samAccountName | Enable-ADAccount
Write-Host "-User "$samAccountName" Enabled"
}

   6. Now run the Enable-Bulk-AD-Users-FromCSV.ps1 file in Powershell to Enable Bulk Active Directory users from CSV file.

PS C:Scripts>  .Enable-Bulk-AD-Users-FromCSV.ps1
Enable Bulk AD Users From CSV file using Powershell Script



Note: I have placed script file in the location C:Scripts, if you placed in any other location, you can navigate to that path using CD path command (like cd “C:\Downloads”).

Thanks,
Morgan
Software Developer


Advertisement

Leave a Comment