Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a group with long name to local group from command prompt or batch file?

When trying to add a global group with a name longer than 20 characters using net.exe I get an error saying that the syntax is incorrect, as follows:

C:\>NET.EXE localgroup MyRemoteUsers "really-long-group-name-here" /ADD

The syntax of this command is:

NET LOCALGROUP [groupname [/COMMENT:"text"]] [/DOMAIN]
groupname {/ADD [/COMMENT:"text"] | /DELETE}  [/DOMAIN]
groupname name [...] {/ADD | /DELETE} [/DOMAIN]

This problem is documented by Microsoft here. I need this to work inside a standard .cmd batch file. Is there a simple workaround?

like image 623
Alex Avatar asked Aug 24 '12 15:08

Alex


People also ask

Which command line tool can be used to create a local group in Windows 10?

In this article To run net localgroup, open a command prompt, type net localgroup with the appropriate parameters, and then press ENTER.

How do I open local users and groups from the command line?

Press the Windows key + R to open the Run dialog box, or open the Command Prompt. Next type lusmgr. msc and hit Enter. This will open the Local Users and Groups snap-in directly.


1 Answers

You can use powershell in a batch file like this:

powershell -command "& { ([adsi]'WinNT://./your-local-group,group').Add('WinNT://YOURDOMAIN/your-really-long-global-group-name,group'); }"

One of the tricks above is to use double quotes for entire command while using single quotes within the commands. This allows you to run the statement from cmd.exe or inside a .bat/.cmd file.

like image 142
Alex Avatar answered Sep 22 '22 02:09

Alex