I am creating a script that should be able to delete a specified Active Directory User. If no user is specified then it will prompt before deleting all users. If the user name is misspelled then it will error and say “User not found” I have the script here but it wont let me search a user for deletion by username, it only lets me use their first name which can be problem some for a bigger business. I also cant get the script to delete all the users. I have been working on this so long that I dream about it. I can’t seem to find anything useful online. Please help? Thank you.
<#
.Synopsis
Locates specified accounts for deletion
.Description
Locates specified accounts for deletion
.Example
Get-DELUSER [username]
#>
$DN=“DC=AD-4313,DC=itnsntc,DC=org”
$Users = Get-ADUser -filter * -SearchBase $DN | Select-Object -Property samaccountname
Function Get-DELUSER
{
#create array to add multiple users for deletion
$temp=@()
do{
$user=Read-Host “Enter the users first name “
if($user -ne ‘’){
$temp += $user+”*”
}
}until($user -eq ‘’)
foreach($user in $temp)
{
#search through the users for each user in $temp array and set $userfullname to that users name.
if(dsquery user -name $user ){
$userfullname = Get-ADUser -filter {name -like $user} -Properties DisplayName | Select-Object Name -ExpandProperty name
#set usersamacctname to the user sam id for deletion
$usersamacctname= Get-ADUser -filter {name -like $user} -Properties Samaccountname | Select-Object samaccountname -ExpandProperty samaccountname
Write-Output “User ‘$userfullname’ will be removed from AD”
#Removes user account from Active Directory.
Remove-ADUser $usersamacctname -Confirm:$false
}
#else command handles errors
else{Write-Output “User ‘$user’ not found”}
}
If ($user -eq ‘’) {
Write-Output "User ‘$Users’ has been removed"
Remove-ADUser $Users -confirm }
}