Help Resetting Entra User Passwords from a CSV – What’s Going Wrong?

0
6
Asked By CuriousCoder92 On

Hey, everyone! I'm trying to reset user passwords for Entra ID from a CSV file using a PowerShell script, but I'm running into some issues. The script is supposed to import a CSV of users, loop through them, and reset their passwords. I'm testing it with just me in the CSV, but I'm not being prompted to change my password when I sign in again. Just so you know, we're a cloud-only setup with all users on Entra and no AD domain. Here's a sneak peek at 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 idea what might be going wrong?

3 Answers

Answered By ScriptSavvy88 On

First off, I noticed you might have your full name in the file path. Just a little heads up! Also, you might need to send the password as a secure string when resetting it.

CuriousCoder92 -

Ouch! Good catch. Thanks for calling that out and pointing me in the right direction.

Answered By CodeNinja77 On

Yeah, you definitely need to use secure strings for the passwords when working with Entra. Check their official documentation for the correct syntax – it really helps!

Answered By DebuggingDude On

Interestingly, the password doesn’t need to be a secure string, but make sure you're using `$true` instead of "True". That’s probably the key issue!

CuriousCoder92 -

This is news to me, nice!

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.