Reputation: 3568
Alright, I need something of a batch scripting guru to help me out of the corner that I've backed myself into.
I have a program that runs as system and I want to change the password for all the accounts that appear in the output for net user. I'm not really sure how I could do this with just command line or AHK-based scripting.
When I perform a net user command the output is like this for me:
C:\Users\Resident>net user
User accounts for \\9100BASELINE
-------------------------------------------------------------------------------
Administrator Guest Resident
The command completed successfully.
I need some way to change the password on all the accounts here (be it 3 or 50) to something of my choosing.
Can anyone help me out with this? I tried slapping together a for loop where each item is a token before I realized that I don't know how to regex the usernames out of there.
Upvotes: 0
Views: 3066
Reputation: 49
You can actually do this purely with batch.
The simplest method would be to list all the folders with:
cd C:\Users
dir /b /o:n /ad > users.txt
After that, you'd use a for loop that sets a password to each user in the Users directory, and makes sure that the script doesn't try to set a password for the Public folder, as it is not a user.
It'd look like this:
for /f %%i in ('type C:\Users\users.txt') do(
if not %%i==Public (
net user %%i [Insert Password Here]
)
)
Technically, you don't actually have to check for extra non user folders, as cmd won't actually cause any problems if it attempts to set a password for a non user folder.
Upvotes: 2
Reputation: 200433
I'd recommend employing the help of a little VBScript:
Set accounts = GetObject("WinNT://.")
accounts.Filter = Array("user")
For Each user In accounts
WScript.Echo user.Name
Next
Save it as listusers.vbs
and run it like this:
@echo off
setlocal
set /p "newpw=Enter new password: "
for /f "delims=" %%u in ('cscript //NoLogo C:\path\to\listusers.vbs') do (
net user "%%u" "%newpw%"
)
Edit: If you want to omit specific accounts from being processed you can either add an exclude list to the VBScript:
Set exclude = CreateObject("Scripting.Dictionary")
exclude.CompareMode = vbTextCompare
exclude.Add "HomeGroupUser$", True
exclude.Add "otheruser", True
...
Set accounts = GetObject("WinNT://.")
accounts.Filter = Array("user")
For Each user In accounts
If Not exclude.Exists(user.Name) Then WScript.Echo user.Name
Next
or filter the output of listusers.vbs
with findstr
:
for /f "delims=" %%u in (
'cscript //NoLogo C:\path\to\listusers.vbs ^| findstr /v /i ^
/c:HomeGroupUser$ /c:otheruser ...'
) do (
net user "%%u" "%newpw%"
)
Upvotes: 1