Link to home
Create AccountLog in
Avatar of bsharath
bsharathFlag for India

asked on

Add a user to all groups in the file

Hi,

I want to add a user or group to all groups which are in the file.

Can i have a script which can do this.

Regards
Sharath
Avatar of Farhan Kazi
Farhan Kazi
Flag of Australia image

Hi Sharath,
Following script will add user to groups that are defined in Groups.txt file at C: drive root.
Change the value of UserName variable to the actual username.
Like: SET UserName=bsharath
Copy and paste following script in notepad and save it with .bat extension.

:: .bat script start
@Echo Off
SET UserName=uname
For /F "delims=;" %%g IN (C:\Groups.txt) Do (
      @Echo Adding user in group: %%g
      Net Group "%%g" %userName% /ADD /DOMAIN
)
:: .bat script end

Hope it will help.
Avatar of bsharath

ASKER

Can this bat file add groups also to the groups in the file?
I get this.

Adding user in group: Users-SGW
The request will be processed at a domain controller for domain Development.iGroup.co.uk.

There is no such user: Chennai_FS-SG

More help is available by typing NET HELPMSG 3755.
I checked i am able to add a user not a group...
Hi Sharath,
Above script will only add user into groups. I am sorry to say that I have no idea how to add group inside another group from command line or through batch file.
With your above script can we add users to all the groups in the file.

I get this error.

Adding user in group: kar_T2Build
The request will be processed at a domain controller for domain Development.Group.co.uk.


The group name could not be found.

More help is available by typing NET HELPMSG 2220.

Adding user in group: kar_T2BA
The request will be processed at a domain controller for domain Development.Group.co.uk.


The group name could not be found.

More help is available by typing NET HELPMSG 2220.

Adding user in group: kar_T2Dev
The request will be processed at a domain controller for domain Development.Group.co.uk.


The group name could not be found.
Avatar of Fridolin Mansmann
Download and install MS Resource Kit Tools, there is a command line tool for managing users:

cusrmgr.exe

OPEN CMD and run the following command:

For /F "delims=;" %g IN (C:\Groups.txt) Do cusrmgr -u DOMAINNAME\USERNAME -m \\DOMAINCONTROLLERNAME -agg "%g"

(with quotes, then it will also work with group names that contain a space character)
Where can i get this file cusrmgr.exe?
I have already downloaded the rktool.exe but here in no cusrmgr.exe file after i extracted it.
Found this:

"Cusrmgr.exe - Console User Manager         This command-line tool enables editing of many of the properties available through the Local Users and Groups, a Microsoft® Windows® 2000 system tool that is part of the Computer Management MMC snap-in. With CusrMgr, you can rename or delete users, set passwords, and set or reset other user properties."

Maybe it is not included any more in the latest version of MS ResourceKit any more?
Please search Google for download
Hi Sharath,

Following script will add users and groups that are specified in Members.txt file on C: drive root.
You need to set 'TargetGroup' variable's value (inside the script) to the name of a Group in which you want to add members.
Copy and paste following script into notepad and save it with anyname having .bat extension.

:: --- Script Start ---
@Echo Off
SET TargetGroup=PrnAccess

IF EXIST C:\GFName.txt DEL /F /Q C:\GFName.txt
IF EXIST C:\UFName.txt DEL /F /Q C:\UFName.txt

FOR /F "delims=#" %%z IN (C:\Members.txt) Do (
      DSQuery User  -name "%%z*" | find /I "CN=%%z" >>C:\UFName.txt
      DSQuery Group -name "%%z*" | find /I "CN=%%z" >>C:\GFName.txt
)


FOR /F "delims=#" %%u IN (C:\UFName.txt) Do (
      DSQuery Group -name "%TargetGroup%*" | DSMod Group -addmbr %%u
)

FOR /F "delims=#" %%g IN (C:\GFName.txt) Do (
      DSQuery Group -name "%TargetGroup%*" | DSMod Group -addmbr %%g
)

IF EXIST C:\GFName.txt DEL /F /Q C:\GFName.txt
IF EXIST C:\UFName.txt DEL /F /Q C:\UFName.txt
:: --- Script End ---

Keep following rules in mind:
------------------------------------
1) Domain Local Groups:
Domain Local security groups can contain user accounts, universal groups, and global groups from any domain.

2) Global Groups:
Global security groups are most often used to organize users who share similar network access requirements. They can include only user accounts in the same domain. Global groups cannot contain local groups or other global groups and are not assigned to local resources.

3) Universal Groups
Universal security groups are most often used to assign permissions to related resources in multiple domains. Members from any domain may be added. The universal scope can contain user accounts, universal groups, and global groups from any domain. The scope can be a member of domain local or universal groups in any domain. Universal security groups are not available in mixed mode.

Hope it will help
What is the file in which i  need to add the target group.

I have created a vbs file and then a members file.Is there any other file i need to create
ASKER CERTIFIED SOLUTION
Avatar of Farhan Kazi
Farhan Kazi
Flag of Australia image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
I am trying to add a user to Global group

I have 5 users in Source.txt and 2 groups in Targetgroups.txt.Once i run the bat file nothing happens.

I check the ads it does not add the users to the group.

Can you please show me what you have in Source.txt and Targetgroups.txt files?
Both files should have CN names Like:
In Source.txt -> Farhan Kazi or Just Farhan
In Targetgroups.txt -> Development Group or just Development

In source file

enoch johnson
aruns
sharath reddy

In target file
Annex-SGR.development.plc.co.uk
In target just add Annex-SGR without "development.plc.co.uk"
I tried evn this but did not work...
Remove last three lines from the script that are:

IF EXIST C:\GFName.txt DEL /F /Q C:\TGFName.txt
IF EXIST C:\GFName.txt DEL /F /Q C:\GFName.txt
IF EXIST C:\UFName.txt DEL /F /Q C:\UFName.txt

Run .bat script and then show me the contents of C:\TGFName.txt,  GFName.txt, C:\UFName.txt files.
Any update ??
Hi sorry fro the delay in responding

Will this script add a user to all groups in the file.?

Or

Will this script add many users to a single group?
All users will be added to all groups specified in a file.