Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. function Update-ADPassword
  2. {
  3. param(
  4. [string]$Domain = $env:USERDOMAIN
  5. )
  6.  
  7. try {
  8. Add-Type -AssemblyName System.DirectoryServices.AccountManagement
  9. $ctx = [System.DirectoryServices.AccountManagement.PrincipalContext]::new('Domain', $Domain)
  10. $acc = [System.DirectoryServices.AccountManagement.UserPrincipal]::FindByIdentity($ctx, ($currCred = Get-Credential -Message 'Input your current credentials').UserName)
  11. if($? -and @($acc.Count) -eq 1) {
  12. $acc.ChangePassword($currCred.GetNetworkCredential().Password, $(Get-Credential -UserName $currCred.UserName -Message "Input your new password").GetNetworkCredential().Password)
  13. Write-Host "Password successfully changed for $($currCred.UserName) in $Domain"
  14. }
  15. else {
  16. if(@($acc).Count){
  17. Write-Warning "No unambiguous account match found in $Domain"
  18. }
  19. else {
  20. Write-Warning "No user account found in $Domain"
  21. }
  22. }
  23. }
  24. catch {
  25. throw
  26. return
  27. }
  28. finally {
  29. Clear-Variable 'currCred' -PassThru |Remove-Variable
  30. }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement