Reputation: 39
I have a powershell script to remove a user stored under the variable $User which is taken from a user input in the command line. How do I specify multiple users and remove all of them?
Script is below
$User = Read-Host - Prompt 'Enter user name'
Remove-ADUser $User
Write-Host "'$user' account has been removed press any key to close..."
$Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Upvotes: 0
Views: 1678
Reputation: 477
perhaps this helps little with getting a more save result. it took me a few minutes to write it. maybe it helps.
######################################
# first make sure we know what is happing..
######################################
$name = 'bob'
$AccountToDelete = Get-ADuser -filter {enabled -eq $true} -properties "displayname" | where {$_.displayname -match $name}
######################################
# then go a step further
######################################
$name = 'bob'
$AccountToDelete = Get-ADuser -filter {enabled -eq $true} -properties "displayname" | where {$_.displayname -match $name}
#show results of filter
$AccountToDelete.name
if ($AccountToDelete.count -gt 1)
{
write-warning 'more then one user:'
$AccountToDelete.name
BREAK
}
ELSE
{
'delete {0}' -f $AccountToDelete.name
Remove-ADUser $AccountToDelete -WhatIf
}
######################################
# improvement 1
######################################
$names = 'bob','don'
foreach ($name in $names){
$AccountToDelete = Get-ADuser -filter {enabled -eq $true} -properties "displayname" | where {$_.displayname -match $name}
#show results of filter
$AccountToDelete.name
if ($AccountToDelete.count -gt 1)
{
write-warning 'more then one user:'
$AccountToDelete.name
BREAK
}
ELSE
{
'delete {0}' -f $AccountToDelete.name
Remove-ADUser $AccountToDelete -WhatIf
}
}
######################################
# improvement 2
######################################
#now add names to delete in a notepad textfile, one name per line
<#
you can use this to create a file
PS c:\users\administator> notepad users.txt
#>
#replace the string arrary $names = 'bob','don'
$names = (get-content .\users.txt).split('^t')
$names
'processing {0} names...' -f $names.count
foreach ($name in $names){
$AccountToDelete = Get-ADuser -filter {enabled -eq $true} -properties "displayname" | where {$_.displayname -match $name}
#show results of filter
$AccountToDelete.name
if ($AccountToDelete.count -gt 1)
{
write-warning 'more then one user:'
$AccountToDelete.name
BREAK
}
ELSE
{
'delete {0}' -f $AccountToDelete.name
Remove-ADUser $AccountToDelete -WhatIf
}
}
#finally if the script is showing you the results you need you can remove the -WhatIf
Upvotes: 1
Reputation: 46
Agree with @Theo but if you know what you are doing there is a simple solution:
$User = Read-Host - Prompt 'Enter user name'
foreach($u in $User.Split(','))
{
Remove-ADUser $u
Write-Host "'$u' account has been removed"
}
$Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
All you need to know is delimiter which you have to use. In that case it is ',', so you need to pass logins in pattern: user1,user2
Upvotes: 2