Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 868
  • Last Modified:

Script to remove users from local admin group on remote computers

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 &""

1 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

desktop-eeAuthor Commented:
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.

Featured Post

Who's Defending Your Organization from Threats?

Protecting against advanced threats requires an IT dream team – a well-oiled machine of people and solutions working together to defend your organization. Download our resource kit today to learn more about the tools you need to build you IT Dream Team!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now