Help with the 'For' command

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Aar318
Posts: 2
Joined: 12 Aug 2015 07:04

Help with the 'For' command

#1 Post by Aar318 » 12 Aug 2015 07:06

So, basically I am trying to run this command to batch delete local admins that are domain users. However, when I get to the below code, it is attempting to run the command on the name of the file as opposed to the file contents (the echo below verified this). The %_localadmins% variable is the name of the text file stored in the variable. I have tried all sorts of variations of the FOR command to no avail. Is there something simple that I am missing here? Thanks for the help!

What it does:
1. Pulls the administrators in the local administrators group
2. Searches the administrators for any names starting with the domain (either FQDN or just DN)
3. Creates a new text file with the names with the DN
4. Attempts to delete the accounts listed in the text file (which is the problematic piece)
5. Looks for a specified local account and creates it if it is missing
6. Adds specified account to the local administrators group

Code: Select all

FOR %%L IN ("%_localadmins%") DO ((net localgroup administrators %%L /delete ) && echo %%L)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help with the 'For' command

#2 Post by foxidrive » 12 Aug 2015 07:29

I added this /f "usebackq delims="

Code: Select all

FOR /f "usebackq delims=" %%L IN ("%_localadmins%") DO ((net localgroup administrators %%L /delete ) && echo %%L)

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: Help with the 'For' command

#3 Post by Squashman » 12 Aug 2015 07:45

Why the nested parentheses?

Aar318
Posts: 2
Joined: 12 Aug 2015 07:04

Re: Help with the 'For' command

#4 Post by Aar318 » 12 Aug 2015 09:22

foxidrive wrote:I added this /f "usebackq delims="

Code: Select all

FOR /f "usebackq delims=" %%L IN ("%_localadmins%") DO ((net localgroup administrators %%L /delete ) && echo %%L)


Thank you very much!! That worked and I have been stressing over it for a while now! What exactly is the usebackq used for? I tried that as well but I guess I didn't do it correctly.

Squashman wrote:Why the nested parentheses?


I was throwing it in there like that to display the output and verify that it was indeed trying to run the name of the file ("local admins.txt") as opposed to the contents of each item (domain\user). I will remove it now that it is working :)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help with the 'For' command

#5 Post by foxidrive » 12 Aug 2015 10:49

Aar318 wrote:What exactly is the usebackq used for?


The help shown using for /? gives a lot of information.

In the for /f type of for command there are a few ways to define text or a command in the for body by using quotes or double quotes or backticks.

usebackq changes those around - and in your case the double quotes change the scope from the filename, to the contents of the file itself.

Post Reply