Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement