Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.