Password Change Script

Modified on Tue, 30 Jun at 3:33 PM

#Define the path to the CSV file

$csvFilePath = "c:\temp\changepass.csv"

 

#Load the CSV data into a variable

$csvData = Import-Csv -Path $csvFilePath

 

#Define force password change after sign in

$ForceChangePasswordNextSignIn = "true"

 

#Loop through each user in the CSV data and update their password

foreach ($user in $csvData) {

    $userPrincipalName = $user.UserPrincipalName

    $userPassword = $user.Password

 

 #   Check if the user exists

    $existingUser = Get-MgUser -UserId $userPrincipalName -ErrorAction SilentlyContinue

 

    if ($null -ne $existingUser) {

        try {

            $params = @{

                PasswordProfile = @{

                    password                      = $userPassword

                    ForceChangePasswordNextSignIn = $ForceChangePasswordNextSignIn

                }

            }

            Update-MgUser -UserId $UserPrincipalName -BodyParameter $params -ErrorAction Stop

 

            Write-Host "Password updated for user: $userPrincipalName" -ForegroundColor Green

        }

        catch {

            Write-Host "Failed to update password for user: $userPrincipalName" $_.Exception.Message -ForegroundColor Red

        }

    }

    else {

        Write-Host "User not found: $userPrincipalName" -ForegroundColor Yellow

    }

}


Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article