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: Windows 2016 Pristine Image

Desired Output Windows Server deployment should be made as seamless and efficient as possible. Here's…

Check Servers NSLookup of a Listener to Match Active Node IP

$servers="SQL01","SQL02","SQL03","SQL04" $listener="halistener01" $activeNode="10.10.10.5" # Dynamic Credential method 1 $who = whoami if ($who.Substring($who.length-2, 2)="-admin"){$username=$who;} else…

PowerShell: Convert Between Various SSL Certificate Formats

# Install Choco (look for instructions in this blog)# Install openssl.lightchoco install openssl.light -y #…