Script to remove users from local admin group on remote computers

Posted on 2011-05-04
Last Modified: 2012-05-11
I have a csv file with a list of computers and users that are added to the local Administrators group of those computers and need a script that will read the csv file and remove the user from that particular computer's local Administrator group.

I was able to find a simple script that will remove a stated user from a stated computer (see below) but again I need something that will read the csv file with users that will be removed from certain computers.

Dim DomainName
Dim UserAccount
Set net = WScript.CreateObject("WScript.Network")
local = "computer name"
DomainName = "domain name"
UserAccount = "user name"

set group = GetObject("WinNT://"& local &"/administrators")
on error resume Next
group.Remove "WinNT://"& DomainName &"/"& UserAccount &""

Question by:desktop-ee
    LVL 8

    Accepted Solution

    Hi desktop-ee

    Try this script out, change the variable sCSVFileName to be the full path to the CSV file you have.

    This script is untested so please test on one PC before going live.

    Const ForReading = 1
    On Error Resume Next
    sCSVFileName = "C:\CSVFile.csv"
    Set oRoot = GetObject("LDAP://RootDSE")
    vDomain = Replace(Replace(oRoot.Get("DefaultNamingContext"), ",DC=", "."), "DC=", "")
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oTextFile = oFSO.OpenTextFile (sCSVFileName, ForReading)
    Do Until oTextFile.AtEndOfStream
    	sLine = oTextFile.ReadLine
    	If Trim(sLine) <> "" Then
    		sPC = Split(sLine, ",")(0)
    		sUser = Split(sLine, ",")(1)
    		Set oGroup = GetObject("WinNT://"& sPC &"/administrators")
    		If Err.Number = 0 Then
    			oGroup.Remove "WinNT://"& vDomain &"/"& sUser &""
    			If Err.Number = 0 Then
    				WScript.Echo sUser & " was removed successfully"
    				WScript.Echo "An error occured - " & Err.Description
    			End If
    			WScript.Echo "PC is currently unavailable"
    		End If
    	End If	 

    Open in new window


    Author Comment

    Apologies for the late response. I tried the above script and it worked perfectly in our test environment. We have to do some more testing on a wider range of computers but I believe this is going to work great. Thank you so much Jawa29.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    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.

    Communication between departments might not happen in two different languages, but they do exist in two different worlds. With different targets and performance goals the same phrase often means something completely different to each party. Learn ho…
    Ever notice how you can't use a new drive in Windows without having Windows assigning a Disk Signature?  Ever have a signature collision problem (especially with Virtual Machines?)  This article is intended to help you understand what's going on and…
    Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
    With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

    759 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

    14 Experts available now in Live!

    Get 1:1 Help Now