Skip to content

Instantly share code, notes, and snippets.

@gjyoung1974
Created December 17, 2019 17:03
Show Gist options
  • Save gjyoung1974/f06f741500f7f2eaa3cff4a66e3de6f1 to your computer and use it in GitHub Desktop.
Save gjyoung1974/f06f741500f7f2eaa3cff4a66e3de6f1 to your computer and use it in GitHub Desktop.
function Set-PasswordRemotely {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)][string] $UserName,
[Parameter(Mandatory = $true)][string] $OldPassword,
[Parameter(Mandatory = $true)][string] $NewPassword,
[Parameter(Mandatory = $true)][alias('DC', 'Server', 'ComputerName')][string] $DomainController
)
$DllImport = @'
[DllImport("netapi32.dll", CharSet = CharSet.Unicode)]
public static extern bool NetUserChangePassword(string domain, string username, string oldpassword, string newpassword);
'@
$NetApi32 = Add-Type -MemberDefinition $DllImport -Name 'NetApi32' -Namespace 'Win32' -PassThru
if ($result = $NetApi32::NetUserChangePassword($DomainController, $UserName, $OldPassword, $NewPassword)) {
Write-Output -InputObject 'Password change failed. Please try again.'
} else {
Write-Output -InputObject 'Password change succeeded.'
}
}
Set-PasswordRemotely
@gjyoung1974
Copy link
Author

Opps! you are locked out of the domain by an expired password.
Use this script to reset your password. The client you use doesn't need to be joined to the domain, or logged on via a domain user.

You simply need to know your former password.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment