Delete local users whose names are in a file.

Posted on 2009-02-16
Last Modified: 2012-05-06

Delete local users whose names are in a file.
I have the machine names in a txt file and the user names also in another file. Check each user is available in each machine and delete the local user account from each machine. Need the success/Failure results too.

Question by:bsharath
    LVL 47

    Assisted Solution

    Have you read this How to remove unwanted local user accounts
    The preferred solution is to delete the local user accounts from each workstation that has them. A possible alternative is to use Group Policy to manipulate the Log On Locally user right to prevent anyone except domain users from logging on to desktop computers targeted by such policy. The Log On Locally user right is found under Computer Configuration \ Windows Settings \ Security Settings \ Local Policies \ User Rights Assignment. But the Log On Locally approach should be carefully tested on a test network before using it on your product network to ensure no unpredictable effects result from implementing it in your environment.
    LVL 14

    Accepted Solution

    i did it very quick but i think it will do the job

    change name and path in this lines:

    Set userlist = fso.OpenTextFile("c:\userlist.txt", 1)
    Set complist = fso.OpenTextFile("c:\complist.txt", 1)
    Set objlog = fso.CreateTextFile("c:\log.txt", 2)
    Set fso = CreateObject("scripting.filesystemobject")
    Set objNetwork = CreateObject("Wscript.Network")
    Set userlist = fso.OpenTextFile("c:\userlist.txt", 1)
    Set complist = fso.OpenTextFile("c:\complist.txt", 1)
    Set objlog = fso.CreateTextFile("c:\log.txt", 2)
    alluserlist = userlist.ReadAll
    Do Until complist.AtEndOfStream
    strComputer = complist.ReadLine
       If Reachable(strComputer) Then
       If per(strComputer) then
    Set colAccounts = GetObject("WinNT://" & strComputer & "")
    colAccounts.Filter = Array("user")
    For Each objUser In colAccounts
        If InStr(alluserlist,objUser.Name) Then
        On Error Resume Next
        colAccounts.Delete "user", objUser.Name
        If Err.Number <> 0 Then
        objlog.WriteLine "error deleting " & objUser.Name & " on " & strcomputer & vbcrlf
        On Error Goto 0
        On Error Goto 0
        objlog.WriteLine "deleting " & objUser.Name & " on " & strcomputer & vbcrlf
        End If
        End If
         objlog.WriteLine "You Don't Have Permission On " & strComputer & vbCrLf
       End if
    objlog.WriteLine strComputer & " Isn't Reachable" & vbCrLf
    End if
    Function Reachable(strComputer)
     strCmd = "ping -n 1 " & strComputer
     Set objShell = CreateObject("WScript.Shell")
     Set objExec = objShell.Exec(strCmd)
     strTemp = UCase(objExec.StdOut.ReadAll)
     If InStr(strTemp, "REPLY FROM") Then
     Reachable = True 
     Reachable = False
     End If
    End Function
    Function per(computer)
            strcomputer = computer
            On Error Resume Next
            Set objWMIService = GetObject("winmgmts:" _
            & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
            If err.number <> 0 Then
                    per = False
                    On Error goto 0
                    per = True
                    On Error goto 0
            End If
    End Function

    Open in new window


    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
    Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now