0

So I have a script that grabs a list of users and then sets a password for all of them, simple enough right? However, When I get the list of users using the wmic output (which is converted to the corrected format using a loop) each user gets an output similar to

    "Administrator      "
    "Guest         "
    "DefaultAdmin       "
    ...

now this list needs to be used in a loop to set the passwords which is a simple for loop to set the passwords, however when you use these users in command prompt it sees them as

    net user "Administrator      " Password

in which case there is not such thing as administrator with 6 spaces

My question is how am I to remove the spaces after the names without manually going in to the text document?

    (for /F "skip=1 delims=" %%X in ('wmic UserAccount where "LocalAccount=True" get Name') do (
        for /f "delims=" %%Y in ("%%X") do (echo %%Y)
    ))>AllUsers.txt
  • Relevant: https://stackoverflow.com/questions/3001999/how-to-remove-trailing-and-leading-whitespace-for-user-provided-input-in-a-batch . – Jeff Zeitlin Nov 30 '17 at 17:53
  • Also relevant: https://stackoverflow.com/questions/16276336/removing-spaces-from-a-variable-in-batch . – Jeff Zeitlin Nov 30 '17 at 17:54
  • And another: https://stackoverflow.com/questions/9310711/remove-trailing-spaces-from-a-file-using-windows-batch . – Jeff Zeitlin Nov 30 '17 at 17:54
  • If you're running a `For` loop against the `WMIC` output, does that not mean you're setting each users' password to the same string? Could you please expand your script example appending your `WMIC` command; thank you. – Compo Nov 30 '17 at 18:24
  • Before asking how to remove the trailing spaces, I would try to avoid them to be generated... Share the code you are creating the list of strings with... – aschipfl Nov 30 '17 at 18:54
  • @aschipfl, unfortunately many WMIC values, this being one, are output as fixed width strings padded with spaces as necessary. – Compo Nov 30 '17 at 19:36
  • @Compo, not necessarily, when you add the `/VALUE` option... – aschipfl Nov 30 '17 at 19:39
  • I'm aware there are options available, @aschipfl, hence the reason I asked to see their WMIC command. – Compo Nov 30 '17 at 19:42
  • Confused on why you are not showing the code you used to get the list of users. – Squashman Nov 30 '17 at 19:57
  • Its supposed to be a same password and theyre going to be set to enter a new password upon logging in @Compo –  Dec 01 '17 at 16:04
  • @Squahman I will add the code in an edit right quick –  Dec 01 '17 at 16:05

1 Answers1

0

If you send your WMIC output via a Call command, it will remove any leading or trailing spaces for you!

@Echo Off
For /F Tokens^=2^Delims^=^" %%A In ('WMIC Path Win32_UserProfile Where^
 "Special!='True'" Assoc /AssocClass:Win32_UserAccount 2^>Nul'
) Do For /F "Skip=1Delims=" %%B In ('WMIC UserAccount Where^
 "SID='%%A' And LocalAccount='TRUE'" Get Name') Do For /F "Tokens=*" %%C In (
    "%%B") Do Call :Sub %%C
Pause
Exit/B

:Sub
Echo=Net User "%*" Password
GoTo :EOF

Remove Echo= from the penultimate line to actually invoke the Net User command

Edit

…and a quick example based upon the method mentioned in aschipfl's comment:

@Echo Off
For /F "Tokens=1*Delims==" %%A In ('WMIC UserAccount Where^
 "LocalAccount='TRUE'" Get Name /Value') Do For /F "Tokens=*" %%C In (
    "%%B") Do Echo=Net User "%%C" Password
Pause
Compo
  • 36,585
  • 5
  • 27
  • 39
  • this works about right, only problem is that it also finds domain users. I don't need to find them for the reason i needed it –  Dec 01 '17 at 16:20
  • the one made by @aschiphls 's comment appears to work as intended with a very slight modification to make sure i wasnt accidentally grabbing a list of thousands of domain users on my network –  Dec 01 '17 at 16:29
  • @IdioticProphet, I have updated both examples to exclude outputting names of non local users. – Compo Dec 01 '17 at 17:23