Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Shell 

How would I cross reference a text file and a list of users then delete users?

How would I cross reference a text file and the computers list of users then delete users that aren't listed in the text file in batch?
The for /f command is used for processing lines of text.

The wmic useraccount list brief command will show a list of user accounts the machine knows about.

Please, stay a while, and learn with us... We will help you to learn how to write code that does what you need. :>
(Feb 1, 2016 07:32 AM)annikk.exe Wrote:  The for /f command is used for processing lines of text.

The wmic useraccount list brief command will show a list of user accounts the machine knows about.

Please, stay a while, and learn with us... We will help you to learn how to write code that does what you need. :>

Thanks for the introduction, I already know C++ but I thought for what I am doing batch would be the best.

Currently this is my logic.
I extract all users separately and put each user into separate strings.
I extract all users separated by the end of line into set of different strings.
Then I cross reference the string array of registered users through the whole array from text file for each user.
Then I prompt to delete user that was not in text file and then delete if yes.

This is what I have so far

Code
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET varFile=vars.txt
SET i=1
FOR /F %%l IN (%varFile%) DO (
    SET line!i!=%%l
    SET /a i=!i!+1
)
ECHO !line1!

ECHO If you see the first line of the text file above the text file was imported sucessfully.
set /p numofrusers=How many users registered?
set /p numoftusers=How many users in text file?

FOR /L %i IN (1,1,%numofusers%) DO (
    FOR /L %m IN (1,1,%numoftusers%) DO (
    IF %USERVARIABLE%%%i compare-op %line%%%i (
            set /p delete=Delete user %line%%%i?
            if %delete%=y net user %line%%%i /del
    )
    )

)
PAUSE

Obviously I don't know how to export users into separate variables yet but that is what I have. Please fix all my mistakes.

The %line%%%i is supposed to call the variable lineI (I being the current for loop time) same principle for %USERVARIABLE%
Code
net user

This shows a list of all the users that have profiles on the machine.

Code
net user fred /delete

This would delete the Fred user.

If you want to do this in a script, it makes sense to test it first.

First, open Regedit.
Expand HKEY_USERS.
Make a note of each key within this folder, for example, you probably have a key called .DEFAULT, and perhaps another called S-1-5-18, or something similar. Write each of these down. Then close regedit.

Now go into Control Panel, Users, and create a new user. Log on as them once, then log back to your regular account.

Open regedit again. In theory, a new key should now exist in HKEY_Users.
Take a note of this key's name. This is the registry hive associated with your new user.

Now open a command prompt and use the net user fred /delete command to delete the user you created. Replace "fred" with the correct username, obviously. :>

After this is done, go into Control Panel, Users, and verify the account is deleted.
You should also check in the Local User Manager - click start, type lusrmgr.msc and check the list of users to verify the account is gone.

Next, open Regedit and check whether the registry hive associated with the user is gone, or still there. Hopefully it's gone. :> If not, we'll need to delete it.
Finally, browse to C:\Users\ and verify that the user's profile folder was deleted.

Some parts may not have been deleted, so it's important to check all of these things, otherwise your script will leave a bunch of junk behind.
Let me know what you find. :>
(Feb 3, 2016 07:03 AM)annikk.exe Wrote:  
Code
net user

This shows a list of all the users that have profiles on the machine.

Code
net user fred /delete

This would delete the Fred user.

If you want to do this in a script, it makes sense to test it first.

First, open Regedit.
Expand HKEY_USERS.
Make a note of each key within this folder, for example, you probably have a key called .DEFAULT, and perhaps another called S-1-5-18, or something similar. Write each of these down. Then close regedit.

Now go into Control Panel, Users, and create a new user. Log on as them once, then log back to your regular account.

Open regedit again. In theory, a new key should now exist in HKEY_Users.
Take a note of this key's name. This is the registry hive associated with your new user.

Now open a command prompt and use the net user fred /delete command to delete the user you created. Replace "fred" with the correct username, obviously. :>

After this is done, go into Control Panel, Users, and verify the account is deleted.
You should also check in the Local User Manager - click start, type lusrmgr.msc and check the list of users to verify the account is gone.

Next, open Regedit and check whether the registry hive associated with the user is gone, or still there. Hopefully it's gone. :> If not, we'll need to delete it.
Finally, browse to C:\Users\ and verify that the user's profile folder was deleted.

Some parts may not have been deleted, so it's important to check all of these things, otherwise your script will leave a bunch of junk behind.
Let me know what you find. :>

omg
I know how to delete a user
I need to know how to translate net users and the usernames into separate variables.
Your loop looks a little screwy here. In a batch file it would be FOR /L %%i. Also numofusers is not defined...should be numofrusers
Code
FOR /L %i IN (1,1,%numofusers%) DO (
    FOR /L %m IN (1,1,%numoftusers%) DO (

I would use a boolean to keep track of a match
Code
for /L %%i in (1,1,%numofrusers%) do (
    set match=false
    for /L %%m in (1,1,%numoftusers%) do (
        IF "%USERVARIABLE%%%i"=="%line%%%i" (
            set match=true
            goto break
        )
    )
    :break
    if !match!==false net user %line%%%i /del
)

REM disclaimer: this is a rough untested code using your variables...mostly
There's no place like ~/
(Feb 3, 2016 12:45 PM)Mr Kake Wrote:  omg
I know how to delete a user
I need to know how to translate net users and the usernames into separate variables.

Gosh, you sound quite frustrated. :S Did you come here and expect someone to just write the code for you?

I talked you through the process I would use to investigate how to do this properly. You don't seem interested though..
(Feb 4, 2016 02:27 AM)annikk.exe Wrote:  Gosh, you sound quite frustrated. :S Did you come here and expect someone to just write the code for you?

I talked you through the process I would use to investigate how to do this properly. You don't seem interested though..

lol no offense but you only talked about how to view the users.
None of your posts gave me any details on translating an output to a variable, especially how to split outputs into different variables. I have done my research but some stuff like how to get a list of variables (usernames) was not anywhere.
(Feb 4, 2016 05:16 AM)Mr Kake Wrote:  lol no offense but you only talked about how to view the users.
None of your posts gave me any details on translating an output to a variable, especially how to split outputs into different variables. I have done my research but some stuff like how to get a list of variables (usernames) was not anywhere.
To do this, you have to do a bit of string manipulation. There's no clear cut answer in batch, so getting the list depends on how creative a coder you are. Long story short, you have to make the script yourself or ask someone really nicely. 🤫 Good luck on your script!


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by AeS0pDec 21, 2020 06:19 AM 0 199 Dec 21, 2020 06:19 AM
Last Post: AeS0p
[C] by saltycrackerMay 29, 2020 04:36 AM 0 138 May 29, 2020 04:36 AM
Last Post: saltycracker
[C] by pebblakOct 15, 2019 05:24 AM 6 303 Oct 15, 2019 05:24 AM
Last Post: Joseph Ducreux