cmm user
cmm user

Reputation: 2606

Error while adding a local group to local user using powershell

I used the following command to set the password and add a group to the user :

$user=[ADSI]'WinNT://localhost/account5';
$user.SetPassword('Passw0rd#');
$OBjOU =[ADSI]'WinNT://localhost/Administrators,group';
$objOU.add($user.path);

The password is getting reset, but I am getting the following error while adding the account to the user :

A member could not be added to or removed from the local group because the member does not exist.

But the local member is already present and it is resting the password of the local user.

Edited :

While trying to add a user to a group manually, the location in which the user is searched is by default a domain.The local user in the machine is not a part of this domain and so it cannot be added. When I select the location as local machine and then add the user, the user is getting added. This might be the reason for getting the above error. How can we change the location(in which to search for users) to the local machine through powershell commands?

Upvotes: 2

Views: 5992

Answers (2)

cmm user
cmm user

Reputation: 2606

I tried the following and it worked fine :

$CompStat = Get-WmiObject win32_computersystem;
$Localhst = $CompStat.Name;
$Computer = [ADSI]('WinNT://'+$localhst+',computer');
$accName = [ADSI]('WinNT://'+$Localhst+'/account8,user');
$group = [ADSI]('WinNT://'+$Localhst+'/testgroup,group');
$group.add($accName.path);

Upvotes: 0

Adil Hindistan
Adil Hindistan

Reputation: 6605

Try this:

$user=[ADSI]"WinNT://$Env:Computername/account5";
$user.SetPassword('Passw0rd#');
$OBjOU =[ADSI]"WinNT://$Env:ComputerName/Administrators,group";
$objOU.add($user.path);

Upvotes: 3

Related Questions