Posted On July 2, 2020

PowerShell: Setting or Resetting User Password

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: Setting or Resetting User Password
$username='dragoncoin'
$newPassword='SomeComplexPasswordHere'

function resetPassword($username,$password){
    if($env:userdnsdomain){
        try{
            Unlock-ADAccount -Identity $username
            Set-ADAccountPassword -Identity $username -Reset -NewPassword (ConvertTo-SecureString -AsPlainText "$password" -Force) -ea Stop
            }
        catch{
            write-warning "$($error[0])"
            try{
                net user /domain $username /active=yes
                net user /domain $username $password
                }
            catch{
                write-warning "$($error[0])"
                }
            }
        }
    else{
        net user $username /active=yes
        net user $username $password
        }
}

Leave a Reply

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

Related Post

PowerShell: Get-NetTCPConnection for Windows 7 & 2008

On Windows 8 & 2012, there's this nifty function named Get-NetTCPConnection that is useful to…

PowerShell: Microsoft SQL Database Migration

Update 9/25/20: There's another version of this script has has been rewritten form scratch. IMO,…

PowerShell: Implementation of Anonymous Self Executing Function

JavaScript has this. It's a little known secret that PowerShell can do this as well.…
Other project: DragonCoin.com