0% found this document useful (0 votes)
10 views2 pages

Delete User From Ad

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 2

To delete a user from Active Directory using PowerShell, you can use

the Remove-ADUser cmdlet. Below is a script example that


demonstrates how to delete a user from Active Directory.
PowerShell Script to Delete a User from Active Directory
powershell
Copy code
# Import the Active Directory module (if not already loaded)
Import-Module ActiveDirectory

# Define the username of the user you want to delete


$userUsername = "johndoe" # Modify this with the actual username of
the user to be deleted

# Confirm deletion with the user


$confirmation = Read-Host "Are you sure you want to delete the user
$userUsername from Active Directory? (Y/N)"
if ($confirmation -eq "Y") {
# Remove the user from Active Directory
Remove-ADUser -Identity $userUsername -Confirm:$false

# Output confirmation
Write-Host "User $userUsername has been deleted successfully."
} else {
Write-Host "User deletion canceled."
}
Explanation of the Script:
1. Import-Module ActiveDirectory: Ensures that the Active Directory
module is loaded and available. This module contains the cmdlets
needed for managing Active Directory.
2. User Identification: The script defines the username of the user
you wish to delete. You can modify the $userUsername variable to
specify the target user's username.
3. Confirmation Prompt: The script uses Read-Host to ask for
confirmation before deleting the user. This is a safety mechanism
to avoid accidental deletions. If you type Y, the deletion will
proceed; if you type anything else, the deletion will be canceled.
4. Remove-ADUser: The cmdlet Remove-ADUser deletes the user
specified by the -Identity parameter. The -Confirm:$false option
bypasses additional confirmation prompts from PowerShell (since
the script already asks for confirmation).
5. Output Confirmation: After the user is deleted, a message is
displayed to confirm the deletion.

Running the Script:


1. Open PowerShell as an Administrator (ensure you have the
necessary permissions to delete users in Active Directory).
2. Copy and paste the script into the PowerShell console or save it
as a .ps1 file and run it.
3. Modify the $userUsername variable with the username of the user
you wish to delete.
4. The script will prompt for confirmation before deleting the user
from Active Directory. If you confirm, it will delete the user;
otherwise, it will cancel the action.

Important Notes:
 Permissions: You must have the necessary permissions to delete
users in Active Directory. Typically, you need to be a member of
the Domain Admins group or have delegated permissions for user
management.
 Irreversible Action: Deleting a user from AD is a permanent
action. Ensure that the user is not required anymore and that
their data has been backed up (if needed) before proceeding.
 User's Home Directory/Files: Deleting a user from Active Directory
will not delete their home directories or any files they may have
stored elsewhere. If you want to delete their files as well, you'll
need to handle that separately (e.g., by removing their home
folder).
Let me know if you need more details or adjustments to the script!

You might also like