Help Needed: Resetting Entra User Passwords with a CSV Script

0
2
Asked By TechNinja42 On

Hey everyone, I'm trying to reset some user passwords in Entra using PowerShell, and I'm having a bit of trouble. I've got a CSV with Entra ID users and my script is supposed to import the CSV, go through each user, and reset their passwords. But when I run the script, it doesn't seem to be working correctly. I'm just testing it with one user (myself), and I'm not prompted to change my password when I log in next. Just to clarify, we're working in a cloud-only setup, so all users are through Entra, and there's no AD domain involved. Any ideas on what might be going wrong here? Here's a snippet of my script:

```powershell
# 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
}
}
```

Any insights would be greatly appreciated!

3 Answers

Answered By CuriousCoder99 On

Looks like the main issue could be how you're defining the `ForceChangePasswordNextSignIn` variable. Instead of using the string "True", you should be using the boolean value `$true`. That might be causing the script to skip forcing the password change on the next sign-in. Also, ensure that your passwords are formatted as secure strings if that's a requirement for the method you're using.

Answered By HelpfulHarry73 On

I noticed a couple of things! First, make sure that your passwords are in a secure string format when you're sending them for password resets. But also, don’t forget that your script path is exposing your full name— it's good for you to know, just in case.

Answered By PowerUser88 On

Actually, I found out that surprisingly, you don't need to send the password as a secure string for this particular API. Confirm if that’s accurate for your case. But sticking to `$true` instead of "True" is definitely what you need to fix.

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.