Skip to content

Instantly share code, notes, and snippets.

@IISResetMe
Last active October 1, 2019 07:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IISResetMe/86772a9e969d74a818aba0ce0ff2e7ab to your computer and use it in GitHub Desktop.
Save IISResetMe/86772a9e969d74a818aba0ce0ff2e7ab to your computer and use it in GitHub Desktop.
function Update-ADPassword
{
param(
[string]$Domain = $env:USERDOMAIN
)
try {
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$ctx = [System.DirectoryServices.AccountManagement.PrincipalContext]::new('Domain', $Domain)
$acc = [System.DirectoryServices.AccountManagement.UserPrincipal]::FindByIdentity($ctx, ($currCred = Get-Credential -Message 'Input your current credentials').UserName)
if($? -and @($acc.Count) -eq 1) {
$acc.ChangePassword($currCred.GetNetworkCredential().Password, $(Get-Credential -UserName $currCred.UserName -Message "Input your new password").GetNetworkCredential().Password)
Write-Host "Password successfully changed for $($currCred.UserName) in $Domain"
}
else {
if(@($acc).Count){
Write-Warning "No unambiguous account match found in $Domain"
}
else {
Write-Warning "No user account found in $Domain"
}
}
}
catch {
throw
return
}
finally {
Clear-Variable 'currCred' -PassThru |Remove-Variable
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment