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?
|
Shell
How would I cross reference a text file and a list of users then delete users? |
|
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. 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 OFFObviously 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 userThis shows a list of all the users that have profiles on the machine. Code net user fred /deleteThis 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: omg I know how to delete a user I need to know how to translate net users and the usernames into separate variables.
Feb 3, 2016 06:35 PM
(This post was last modified: Feb 3, 2016 07:12 PM by b00st3d.)
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 (I would use a boolean to keep track of a match Code for /L %%i in (1,1,%numofrusers%) do (
There's no place like ~/
Feb 4, 2016 02:27 AM
(This post was last modified: Feb 4, 2016 02:28 AM by annikk.exe.)
(Feb 3, 2016 12:45 PM)Mr Kake Wrote: omg 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? 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 10:33 AM
(This post was last modified: Feb 4, 2016 10:33 AM by jahwi.)
(Feb 4, 2016 05:16 AM)Mr Kake Wrote: lol no offense but you only talked about how to view the users.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! |
|
|




![[Image: 0UhKI.gif]](https://i.imgur.com/0UhKI.gif)