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: File Server Cluster Migration Scripts

<# File-Server-Migration-Intra-Domain.ps1 Purposes: 1. Create Volumes with Labels 2. Create Virtual Clustered File Servers 3.…

PowerShell RandomPassword Function

function randomPassword{    # return a reasonably randomized password that starts with "KimConnect:" plus 25…

PowerShell: Find Guest VMs Associated with a Certain Storage Path

# findGuestMvsByStorage.ps1 $storagePath='\\SMBSERVER009' function getAllGuestVms($clusterName){ try{ Import-Module FailoverClusters $clusterName=if($clusterName){ $clustername }else{ (get-cluster).name } $allHyperVHosts={(Get-ClusterNode -Cluster…
Other project: DragonCoin.com