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

active-directory, batch-file, powershell

Solution

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.

Problem

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?

Original source