Author Posts

July 26, 2016 at 11:15 am

Hi All,
I am new to powershell. I created a code to add membership to an existing account. However, account is a member of Users group by default so i tricked the code but it gives me an error
if($local_groups){$groups="$local_groups" -split(",");foreach($group in $groups){ if($group -ne 'Users') {net localgroup $group $local_acc /add} }};

ERROR: + invoke-command -session $session_993 -scriptBlock {if (){$groups="" -split(",...
+missing condition in if statement after 'if ('
At line:1 chat:57

I am stuck and clueless.. Can you please assist?

Thanks a lot in advance,

  • This topic was modified 2 years ago by  NewBe.

July 26, 2016 at 1:10 pm

You could always use the ADSI provider and do something like this:

$Group = [ADSI]"WinNT://Server01.contoso.com/Administrators,group"
$User = [ADSI]"WinNT://Server01.contoso.com/UserName,user"
$Group.Add($User.Path)

July 27, 2016 at 6:11 am

Thanks for your response Paul.these are the local groups and local accounts only.

July 27, 2016 at 3:43 pm

I wrote something a while back that was done with Sapien Powershell studio (first gui I have ever done) and I posted this on script center.
https://gallery.technet.microsoft.com/scriptcenter/2523b208-2f4a-4b7a-a458-1340e3967636

This script adds users to local groups using WMI. You can also see this same script on Git Hub GIST post as well:

Hope this gets you what you need.

July 27, 2016 at 3:46 pm

Because the script is hitting wmi it'll require standard slashes when calling add delete or list:

In the form of: group/username in the gui tool.

July 27, 2016 at 5:13 pm

What Paul posted is for local accounts. Be careful when tricking scripts.