Last active
October 1, 2019 07:56
-
-
Save IISResetMe/86772a9e969d74a818aba0ce0ff2e7ab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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