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: Fix Clustered Disk Errors

# fixClusterDiskErrors.ps1function selectClusterName{ param($domainName=$env:USERDNSDOMAIN) write-host "Now scanning $domainName for all available cluster names. This may…

The 5 Rules of Thumb of Business Presentation (To Attract Investors)

I'm noting this here so that I can revisit these bullet points in the future...…

BASH: Basic AWK Training

At first, AWK resembles the first three letter of 'awkward'. Nope, it's a scripting language…