2

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?

2 Answers 2

3

Try this:

$user=[ADSI]"WinNT://$Env:Computername/account5";
$user.SetPassword('Passw0rd#');
$OBjOU =[ADSI]"WinNT://$Env:ComputerName/Administrators,group";
$objOU.add($user.path);
Sign up to request clarification or add additional context in comments.

9 Comments

+1 Apparently you must use the actual hostname for the user object. Neither localhost nor . will work. For the group you can use either $env:COMPUTERNAME, localhost or ., though. Thanks for yet another inconsistency, Microsoft.
I tried the above script on a remote machine. I am getting the following exception : The following exception occurred while retrieving member "SetPassword": "The network path was not found. " + CategoryInfo : NotSpecified: (:) [], ExtendedTypeSystemException + FullyQualifiedErrorId : CatchFromBaseGetMember
It's not meant to be run on a remote machine. You should run it locally and I know it works, as I use it. As you were using 'localhost' before, I assume you want to run it locally anyway, right?
No. The script was being run in a remote machine. Giving localhost would run the command in the remote machine.
I have tried the same in local machine.It is giving the same error. @AdilH. : I observed something while adding user to a group using GUI. I have added it in the question
|
0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.