Help Me Figure Out Why My Entra Password Reset Script Isn’t Working

0
8
Asked By CuriousCoder89 On

Hey everyone! I'm having some trouble with a script I wrote to reset passwords for Entra ID users from a CSV file. Here's what I'm trying to do: I import a CSV that lists users, iterate through that list, and reset each user's password. I'm testing it using just myself in the CSV. The issue is, after running the script, I don't get prompted to change my password the next time I log in. Just to give you some context, we operate in a completely cloud-based environment, so all our users are managed via Entra without any connection to an AD domain. I'm not sure where I'm going wrong. Here's a snippet of my script for reference:

# Define path to CSV
$csvFilePath = "C:Userspwd-rst.csv"

# Load CSV data into variable
$csvData = Import-Csv -Path $csvFilePath

# Define force password change after sign-in
$ForceChangePasswordNextSignIn = "True"

# Loop through users in CSV and update their password
foreach ($row in $csvData) {
$userPrincipalName = $user.UserPrincipalName
$userPassword = $user.Password

# Check if 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
}
}

3 Answers

Answered By ScriptyMcScriptface On

First off, just a heads up—your full name is visible in the file path of your code. If that's okay with you, no worries, but it's something to think about.
Also, you’ll want to make sure you're sending the password as a secure string. That could be part of the issue here.

Answered By TechWiz347 On

I believe when you're sending the password, it needs to be a secure password string for Entra. Just double-check the syntax over here: https://learn.microsoft.com/en-us/powershell/module/microsoft.entra/set-entrauserpassword?view=entra-powershell. Make sure you’re using a secure string as required.

Answered By CodeNinjaJ On

Actually, surprisingly, the password doesn’t need to be a secure string. The issue might be that you’re using "True" instead of $true in your script. Check this link for more details: https://learn.microsoft.com/en-us/graph/api/resources/passwordprofile?view=graph-rest-1.0. That should help!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.