Error DataBase-One Place all Solutions Forums Blog Glossary    Contact Us
Search  
   
Browse by Category
Error DataBase-One Place all Solutions .: Operating Systems .: Windows Operating Systems .: Windows Scripts and Batch Files .: VBScript to delete all disabled users on a computer

VBScript to delete all disabled users on a computer

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


How helpful was this article to you?

Related Articles

article VB Script to Read a list of users from a spreadsheet, determine if they exist and if they're disabled, then write the result to the spreadsheet
  On Error Resume Next

(No rating)  3-24-2008    Views: 138   
article VBScript to Add user account or group to local computer group
This script allows you to add user accounts...

(No rating)  5-2-2008    Views: 163   
article How to : Add users to the Remote Desktop Users group
To add users to the Remote Desktop Users...

(No rating)  2-21-2008    Views: 189   

User Comments

Add Comment
No comments have been posted.