I'm trying to create new users with powershell. We're not running active directory (not sure if that changes things or not). Here's what I'm trying to do:
$machine = [ADSI]"WinNT://localhost,computer"
$newUser = $machine.Create("User", $Username)
$newUser.setpassword($Password)
$newUser.SetInfo()
Everything works up to this point and the user is created. But now I want to change additional settings like this, but they all fail
$newUser.Put("sAMAcountName", $Username)
$newUser.SetInfo()
$newUser.Put("userAccountControl", 0x10000)
$newUser.SetInfo()
UPDATE
This is the error I'm getting
Exception calling "Put" with "2" argument(s): "Exception from HRESULT: 0x8000500F"
Any idea what I'm doing wrong? Thanks!
Solution
JPBlanc's answer helped point me in the right direction.
The biggest problem is that there's is little to no documentation on using [ADSI]
on machines that are not part of an Active Directory domain.
I was able to solve the issue using the UserFlags
property.
$newUser.UserFlags = $UserFlags.DONT_EXPIRE_PASSWD
$newUser.CommitChanges()
Can you try as administrator :
According to the followin code I cant see
sAMAcountName
oruserAccountControl
which are AD user attributes :