Link to home
Start Free TrialLog in
Avatar of mcompton69
mcompton69

asked on

A Script to Remove a Local User from a Local Group, Where the User has a Unique Name

We have a local user added to the local Administrators group on all our PCs that we want to remove and disable.

The problem is that this user has a unique name related to the PC name.  Is the computer name is ssxp12305 then the local user will be named ss12305xp.

How can i write a script (preferably vbs) that will use wildcards - or is there an alternative method?

thanks.
Avatar of SteveGTR
SteveGTR
Flag of United States of America image

Not VBS, but you could try something like this via a group policy script:

@echo off

setlocal

set adminUser=%computername:~0,2%%computername:~4,5%%computername:~-2,2%

echo net user %adminUser% /DELETE
      
I've disabled the delete using the echo command for testing.
Avatar of mcompton69
mcompton69

ASKER

Thanks Steve, nearly there!!!!

The syntax must be slightly inaccurate on line 3, because when i run the batch file adminUser = SS1230505

I have never seen the commands ":~0,2%" etc. before so i cant alter them as i dont know what they should be!!!!

Can you advise again?  Thanks.
Yes, that was an error by me, sorry :(

@echo off

setlocal

set adminUser=%computername:~0,2%%computername:~4,5%%computername:~2,2%

echo net user %adminUser% /DELETE

For more information on this look at this:

set /?
Thanks SteveGTR

I have just realised a problem though, the original question says i need it disabling, not deleting - to expand on this, it must be disabled rather than deleted because it is the local admin account whcih cannot be deleted.

Can you disable a local user through a batch file?  I tried replacing the /DELETE switch with /DISABLE but of course it doesnt work!
I guess I was confused as well by the "remove and disable" statement you used in your original question. But, I do understand your problem. I'll look into this.
ASKER CERTIFIED SOLUTION
Avatar of SteveGTR
SteveGTR
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
here's an alternative method,
do you want to remove or disable the account?


here's my code, and the last line will be different depending if you want to disable or remove the account



To DISABLE the account after removing from a group:

@echo off
set /p User= Enter the account you want to remove from a group:
echo.
echo.
echo Press any key to display a list of groups you can choose from
pause >nul
echo.
echo.
net localgroup
set /p Group= Enter the group you want to remove the user "%User%" from:
net localgroup %Group% %User% /Delete
net user %User% /Active:No




to DELETE the account after removing it from a group:


@echo off
set /p User= Enter the account you want to remove from a group:
echo.
echo.
echo Press any key to display a list of groups you can choose from
pause >nul
echo.
echo.
net localgroup
set /p Group= Enter the group you want to remove the user "%User%" from:
net localgroup %Group% %User% /Delete
net user %User% /Delete

Thanks both of you, some very useful code here.  However, Steve you provided the actual solution to my problem so points awarded to you.