Skip to content

Instantly share code, notes, and snippets.

@chryzsh
Created May 7, 2019 08:25
Show Gist options
  • Select an option

  • Save chryzsh/f814a3d6088c5bc8f1adfafce2eb3779 to your computer and use it in GitHub Desktop.

Select an option

Save chryzsh/f814a3d6088c5bc8f1adfafce2eb3779 to your computer and use it in GitHub Desktop.
Change an expired password remotely without Interactive access as that user. The method above is actually based on NetUserChangePassword function.
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.'
}
}
@user18081972
Copy link

I dont understand this script. If I run it, it tries to change the password of the computer im running it on.
But i wanna change the password of a remote PC. But I cant run this script directly on the remote PC, because I cant access it because the password is expired.

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