Dharam
Dharam

Reputation: 479

Adding Active Directory group with spaces in name gives error in PowerShell script

I have following command to add AD group in PowerShell script but AD group has space in name and that is causing an issue.

Set-Variable -Name APP_GROUP -Value My AD Group
Add-LocalGroupMember -Group "Administrators" -Member "MyDomain\"$APP_GROUP

Error: CategoryInfo: NotSpecified: RemoteException
+ FullyQualifiedErrorId : NativeCommandError

Upvotes: 1

Views: 7919

Answers (1)

TessellatingHeckler
TessellatingHeckler

Reputation: 28983

Quote it in single quotes, because it's a plain string with nothing else happening, and put the double quotes around the whole thing in the second line (double-quotes because it has a variable name in it which PowerShell needs to expand):

Set-Variable -Name APP_GROUP -Value 'My AD Group'
Add-LocalGroupMember -Group "Administrators" -Member "MyDomain\$APP_GROUP"

It would be more typical to see it written:

$APP_GROUP = 'My AD Group'
Add-LocalGroupMember -Group "Administrators" -Member "MyDomain\$APP_GROUP"

Upvotes: 3

Related Questions