This script will delete all disabled users on a computer.
' You also have an option of listing all the disabled users so
' you can spot check them. You have the option of adding
' usernames you do not want to be deleted if they are disabled.
' I have also pre-added some "Restricted" username from being
' deleted as well.
'
' Script Usages:
' Delete Disabled Users: cscript deldisabledusers.vbs /computer:[computername]
' List Disbaled Users: cscript deldisabledusers.vbs /list:[computername]
'*************************************************************
Dim args,computer
Set args = Wscript.Arguments.Named
computer = args.item("computer")
list = args.item("list")
if wscript.arguments.count = 0 then
wscript.echo "Script Usages:"
wscript.echo "Delete Disabled Users cscript deldisabledusers.vbs /computer:[computername]"
wscript.echo "List Disbaled Users cscript deldisabledusers.vbs /list:[computername]"
elseif args.exists("computer") then
wscript.echo "Starting to delete disabled users..."
Call deleteUsers(computer)
elseif args.exists("list") then
wscript.echo "Starting to list disabled users..."
Call listUsers(list)
end if
Function deleteUsers(computer)
Dim Container,count,user
count = 0
Set Container = GetObject("WinNT://" & computer)
container.filter = Array("User")
' Loops thru the users on server
For Each User in Container
if User.AccountDisabled = True then
Select Case user.name
'Copy 2 lines below and uncomment line to be used for any other users you wish not to be deleted
'Case "USERNAMEGOESHERE"
' wscript.echo "User " & user.name & " is a Restricted Username and can not be deleted"
Case "Guest"
wscript.echo "User " & user.name & " is a Restricted Username and can not be deleted"
Case "Administrator"
wscript.echo "User " & user.name & " is a Restricted Username and can not be deleted"
Case "ASPNET"
wscript.echo "User " & user.name & " is a Restricted Username and can not be deleted"
Case "IUSR_" & ucase(computer)
wscript.echo "User " & user.name & " is a Restricted Username and can not be deleted"
Case "IWAM_" & ucase(computer)
wscript.echo "User " & user.name & " is a Restricted Username and can not be deleted"
Case "TsInternetUser"
wscript.echo "User " & user.name & " is a Restricted Username and can not be deleted"
Case Else
count = count + 1
call container.delete ("User",User.Name)
wscript.echo "User " & user.name & " has been deleted."
End Select
end if
Next
Set Container = nothing
wscript.echo vbcrlf & count & " disabled users have been deleted."
End Function
Function listUsers(computer)
Dim Container,count,user
count = 0
Set Container = GetObject("WinNT://" & computer)
container.filter = Array("User")
' Loops thru the users on server
For Each User in Container
if User.AccountDisabled = True then
count = count + 1
wscript.echo user.name
end if
Next
Set Container = nothing
wscript.echo vbcrlf & count & " disabled users."
End Function