Disable Active Directory user using Powershell script
Note: Make sure you have the Active Directory module installed and authenticated before running the script.
Open PowerShell ISE as an Administrator
Paste below script and run it.
# Import the Active Directory module
Import-Module ActiveDirectory
# Specify the username
$userName = "UsernameToDisable"
# Disable the user account
Disable-ADAccount -Identity $userName
Import the Active Directory module: This line imports the necessary Active Directory module into your PowerShell session. The module provides cmdlets (command-line tools) for managing Active Directory objects.
Specify the username: Replace "UsernameToDisable"
it with the actual username of the account you want to disable. This is the user account that you want to deactivate.
Disable the user account: The Disable-ADAccount
cmdlet is used to disable the specified user account. The -Identity
parameter takes the username you specified earlier, and the cmdlet will disable the user account associated with that username.
Before running the script, make sure you have fulfilled the following prerequisites:
Active Directory Module: You need to have the Active Directory module installed. This module might not be installed by default on all systems. You can typically install it through the “Add Roles and Features” option in the Windows Server Manager or by installing the Remote Server Administration Tools (RSAT) on Windows client machines.
Authentication: You need to be authenticated and have the necessary permissions to modify user accounts in the Active Directory domain. This usually requires administrative privileges.
PowerShell Execution Policy: Ensure that your PowerShell execution policy allows running scripts. You might need to set your execution policy to at least “RemoteSigned” to allow running scripts that are locally created.
Active Directory Connection: Before running the script, you should establish a connection to your Active Directory environment using appropriate credentials. This might involve using the Connect-ADServer
or Get-Credential
cmdlets, depending on your setup.
Thank you for reading!🙏 If you enjoyed this article and want to stay updated with more content like this, follow me on my social media channels:
- YouTube: Techwithpatil
- LinkedIn: Tech with Patil
- Instagram: techwithpatil
Feel free to connect, and let’s continue the conversation!😊