'****************************************************
'		Script Witten by Larry Heintz
'		April 2006 www.windowsadminscripts.com
' This script will allow you to delete a user(s) from
' your local computer or remote computer. There are 2
' ways of doing this, the 1st use the /user option
' and provide the username. The 2nd is use the /filename
' option and provide the filename in the script. Also
' when using the filename option all users must be on
' there own line and the file must be in the same
' directory as the script file. There is also an
' option to list all users on the local or remote
' computer.
'
' Script Usage:
' Delete Single User:
'  cscript deluser.vbs /computer:[computername] /user:[Username]
' Delete Multiple Users:"
'  cscript deluser.vbs /computer:[computername] /filename:"[filename]"
' List All Users:
'  cscript deluser.vbs /computer:[computername] /list
'****************************************************
Dim args,computername,user,filename,list
Set args = Wscript.Arguments.Named
computername = args.Item("computer")
user = args.Item("user")
filename = args.Item("filename")
list = args.Item("list")

if computername = "" then
	computername = getComputer()
else
	computername = computername
end if

if wscript.arguments.count = 0 then
	Call displayUsage()
elseif args.exists("list") then
	Call enumUsers(computername)
elseif args.exists("user") then
	if user = "" then
		wscript.echo "The script has ended due to no User given"
		wscript.quit
	elseif len(user) > 20 then
		wscript.echo "The script has ended due to the lenght of the User given is more than "
		wscript.echo "20 characters. A Windows Username can not be more that 20 characters long."
		wscript.quit
	else
		user = trim(user)
	end if
	Call deleteUser(computername,user)
elseif args.exists("filename") then
	if filename = "" then
		wscript.echo "The script has ended due to no Filename given"
		wscript.quit
	else
		filename = trim(filename)
	end if
	Set fso = CreateObject("Scripting.FileSystemObject")
	if fso.FileExists(getFilePath() & "\" & filename) then
		Call getFile(computername,filename)
	else
		wscript.echo "The script has ended due to the " & filename & " File not existing "
		wscript.echo "in the same folder as the script located in " & getFilePath()
		wscript.quit
	end if
	Set fso = nothing
else
	Call displayUsage()
end if

Function enumUsers(computername)
On Error Resume Next
Dim objusers,euser,count
count = 0
wscript.echo "Starting script..."
Set objusers = GetObject("WinNT://" & computername)
if not (errorChecking (computername)) then
	objusers.filter = Array("user")
		For Each euser in objusers
			count = count + 1
			wscript.echo euser.name
		next
end if
Set objgroup = nothing
wscript.echo VbCrLF & "There are " & count & " Users on " & ucase(computername)
wscript.echo "Ending script..."
End Function

Function deleteUser(computername,username)
On Error Resume Next
Dim objusers
Set objusers = GetObject("WinNT://" & computername)
if not (errorChecking (computername)) then
	Select Case lcase(username)
	'Copy 2 lines below and uncomment line to be used for any other users you wish not to be deleted
		'Case lcase("USERNAMEGOESHERE")
		'	wscript.echo "User " & username & " is a Restricted Username and can not be deleted"
		Case lcase("Guest")
			wscript.echo "User " & username & " is a Restricted Username and can not be deleted"
		Case lcase("Administrator")
			wscript.echo "User " & username & " is a Restricted Username and can not be deleted"
		Case lcase("ASPNET")
			wscript.echo "User " & username & " is a Restricted Username and can not be deleted"
		Case lcase("IUSR_" & ucase(computername))
			wscript.echo "User " & username & " is a Restricted Username and can not be deleted"
		Case lcase("IWAM_" & ucase(computername))
			wscript.echo "User " & username & " is a Restricted Username and can not be deleted"
		Case lcase("TsInternetUser")
			wscript.echo "User " & username & " is a Restricted Username and can not be deleted"
		Case Else
			Call objusers.delete("User",lcase(username))
			wscript.echo "The user " & username & " has been deleted from " & ucase(computername)
	End Select
end if
Set objusers = nothing
End Function

Function getFile(computername,filename)
Dim fso,objfso,tempA,tempB,i
wscript.echo "Starting script..."
Set fso = CreateObject("Scripting.FileSystemObject")
Set obj = fso.OpenTextFile(getFilePath() & "\" & filename)
	tempA = obj.readall
	tempB = split(tempA,vbcrlf)
	i = 0
	for i = 0 to ubound(tempB)
		Call deleteUser(computername,tempB(i))
	next
Set obj = nothing
Set fso = nothing
wscript.echo "Ending script..."
End Function

Function getFilePath()
Dim temp,temp2
temp = split(wscript.scriptfullname,"\")
for i = 0 to ubound(temp) - 1
	temp2 = temp2 & temp(i) & "\"
next
getFilePath = temp2
End Function

Function getComputer()
	Dim objNet
	Set objNet = WScript.CreateObject("WScript.Network") 
	getComputer = objNet.ComputerName 
	Set objNet = Nothing 
End Function

Function errorChecking(computername) 
errorChecking = False 
if err.number <> 0 then 
	wscript.echo "Unable to connect to " & ucase(computername)
	err.Clear () 
	errorChecking = True
	wscript.quit
end if 
End Function

Function displayUsage()
	wscript.echo "Script Usage:"
	wscript.echo "Delete Single User:"
	wscript.echo " cscript deluser.vbs /computer:[computername] /user:[Username]"
	wscript.echo "Delete Multiple Users:"
	wscript.echo " cscript deluser.vbs /computer:[computername] /filename:""[filename]"""
	wscript.echo "List All Users:"
	wscript.echo " cscript deluser.vbs /computer:[computername] /list"
	wscript.quit
End Function
