Posted On October 22, 2021

PowerShell: Reset Active Directory User Password

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: Reset Active Directory User Password
# User input variables
$adminUsername='intranet\kim-a'
$adminPassword='SOMECOMPLEXPASSWORD'
$userId='kim'
$newPassword='SOMECOMPLEXPASSWORD'
$domainController='intranet.kimconnect.com'

# Auto-gen Variables
$encryptedPassword=ConvertTo-SecureString $adminPassword -AsPlainText -Force
$adminCredentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $adminUsername,$encryptedPassword

function resetUserPassword{
  param(
    $userId,
    $newPassword,
    $changeAtNextLogon,
    $adminCredentials,
    $domainController=$env:USERDNSDOMAIN
  )
  $psSession=if($adminCredentials){
    new-pssession -computername $domainController -credential $credentials
  }else{
    new-pssession -computername $domainController
  }  
  $scriptBlock={
    param($userId,$newPassword,$changeAtNextLogon)
      try{
        $newSecuredPassword=(ConvertTo-SecureString -AsPlainText $newPassword -Force)
        Set-ADAccountPassword -Identity $userId -Reset -NewPassword $newSecuredPassword -PassThru -Confirm:$false
        Unlock-ADAccount -Identity $userId
        if($changeAtNextLogon){
          Set-ADUser -ChangePasswordAtLogon $true -Identity $userId -Confirm:$false -verbose
        }
        write-host "$userId's password has been reset to $newPassword successfully!"
        return $true
      }catch{
        write-warning $_ 
        return $false
      }  
  }
  if($psSession.State -eq 'Opened'){
    $result=invoke-command -session $psSession -scriptblock $scriptblock -args $userId,$newPassword,$changeAtNextLogon
    remove-pssession $psSession
    return $result
  }else{
    write-warning "$env:computername is unable to init WinRM into $domainController"
    return $false
  }  
}

resetUserPassword `
  -userId $userId `
  -newPassword $newPassword `
  -changeAtNextLogon $false `
  -adminCredentials $adminCredentials `
  -domainController $domainController
# Sample Output - failure
PS C:\Windows\system32> resetUserPassword $userId $newPassword $false $adminCredentials $domainController
WARNING: The password does not meet the length, complexity, or history requirement of the domain.
False

# Sample Output - success
PS C:\Windows\system32> resetUserPassword $userId $newPassword $false $adminCredentials $domainController
kim password has been reset to $newPassword successfully!
True

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

PowerShell: Performing System Disk Cleanup

Version 2: # cleanupWindows.ps1 # Version 0.0.2 ################################## Excuting Program as an Administrator #################################### #…

PowerShell: Disable Microsoft Dynamics CRM Organization

# disableCrmOrganization.ps1 $orgname='testOrg' $domain='kimconnect.com' $dnsServer='10.10.10.10' $credentials=@{ 'kimconnect\crmAdmin'='password'; 'kimconnect\devAdmin'='password'; } $usernamePrefix='kimconnect\crm' $servernameSuffix='-app01' $ipDictionary=@{ 'dev01'='1.1.1.1'; 'prod01'='2.2.2.2'; }…

PowerShell: How to Create Multiple Arrays or Columns from an Array

# This snippets scans the localhost for common ports and outputs 4 arrays $limitPorts=10000 $netstat=netstat…