0

I'm trying to add the group "Domain Users" to the "administrators" local group in PowerShell using a .ps1 file. The command I'm using is below:

add-localgroupmember -group "Administrators" -Member "DOMAIN\Domain Users"

Strangely, when I run this by itself in a powershell window it works perfectly. However, when it's run via a .ps1 file or a batch file that calls the .ps1 file I get the error below:


There is no such global user or group: DOMAIN\domain.

System error 1388 has occurred.

A new member could not be added to a local group because the member has the wrong account type.

It seems to cut off the "users" part of the group name. I can't seem to find a fix anywhere, hopefully this is a simple one (and sorry if it's a dumb question!)

  • 1
    As for calling from a batch file: When calling PowerShell's _CLI_ (`powershell.exe` for _Windows PowerShell_, `pwsh` for _PowerShell (Core) 7+_) from the outside, using (possibly implied) `-Command` / `-c`, you need to _escape_ `"` chars. you want passed through as part of the command: `\"` works in principle, but can break when calling from `cmd.exe`. In that case, use `"^""` (sic) with `powershell.exe`, and `""` with `pwsh.exe`, inside overall `"..."` quoting. See [this answer](https://stackoverflow.com/a/49060341/45375) for details. – mklement0 Apr 13 '23 at 21:34
  • 1
    By contrast, placing the command inside a `.ps1` file would _not_ produce the symptom you describe. – mklement0 Apr 13 '23 at 21:35

0 Answers0