1

I want to change the password of the current logged in Windows user (no Active Directory) in a scriptable way. The users have the right to change their own passwords and already can change the password via GUI. But I've no way to integrate this in a script.

I've tried net use %user% %newpassword% but that only seems to work if the current user has admin rights.

I've also tried a powershell script:

param (
    [string]$oldPassword = $( Read-Host "Old Password"),
    [string]$newPassword = $( Read-Host "New Password")
)

$MethodDefinition = @'
[DllImport("netapi32.dll", CharSet = CharSet.Unicode)]
public static extern bool NetUserChangePassword(string domainname, string username, string oldPassword, string newPassword);
'@

$NetAPI32 = Add-Type -MemberDefinition $MethodDefinition -Name 'NetAPI32' -Namespace 'Win32' -PassThru

$NetAPI32::NetUserChangePassword('.', $env:username, $oldPassword, $newPassword)

This gives "True" but nothing changes.

Has anyone an idea how I can change the password via script?

  • There are some modules that would help you with that...for example: gallery.technet.microsoft.com/scriptcenter/… – Tim Ferrill Mar 25 '15 at 16:43
  • Have you tried replacing '.' with $env:computername ? – Mathias R. Jessen Mar 25 '15 at 18:14
  • Thank you for the link to the script center. I've tried following: $user = $env:username $password = "xxxxxxxxxx" $computer = $env:computername $user = [adsi]"WinNT://$computer/$user,user" $user.SetPassword($Password) $user.SetInfo() but I always get access denied. – Colorando Mar 25 '15 at 19:42
  • Also tried to replace '.'with $env.computername but nothing changed. I always get just true as result. – Colorando Mar 25 '15 at 19:44
1

I have found the answer:

$oldpw = "oldpassword"
$newpw = "newpassword"
$user = $env:username
$computer = $env:computername
$user = [adsi]"WinNT://$computer/$user"
$user.ChangePassword($oldpw, $newpw)

This worked for me. Thank you for your replies!

0

I don't have enough points here to change it, but this question looks like a duplicate of this one.

  • No, in the linked question it's about changing password for user if in active directory. I don't use active directory. – Colorando Mar 25 '15 at 19:25
  • "On Windows Server 2008 R2, I have a standard (non-administrator) local user (not an Active Directory account, though the server is in a domain) " It's about a local account for an Active Directory member server (non DC). Those are treated the same as local accounts on standalone machines. If the answer in the other thread works, then it should apply here as well. – GuitarPicker Mar 26 '15 at 2:54

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.