For targeted password changes, you can process specific users from a CSV file. This approach is useful when you need to force password changes for specific departments, groups, or security incidents.
First, create a CSV file with the following structure:
UserPrincipalName,DisplayName,Department
user1@yourdomain.com,John Doe,IT
user2@yourdomain.com,Jane Smith,HR
user3@yourdomain.com,Bob Johnson,Finance
Save this as 'users.csv' in C:\Temp\ directory. Then process the users:
# Import users from CSV
$CSVPath = "C:\Temp\users.csv"
if (Test-Path $CSVPath) {
$UsersFromCSV = Import-Csv $CSVPath
Write-Host "Imported $($UsersFromCSV.Count) users from CSV" -ForegroundColor Cyan
# Process each user from CSV
$CSVResults = @()
$UsersFromCSV | ForEach-Object {
try {
Update-MgUser -UserId $_.UserPrincipalName -PasswordProfile @{
ForceChangePasswordNextSignIn = $true
}
$CSVResults += [PSCustomObject]@{
UserPrincipalName = $_.UserPrincipalName
DisplayName = $_.DisplayName
Department = $_.Department
Status = "Success"
ProcessedTime = Get-Date
}
Write-Host "✓ Updated: $($_.UserPrincipalName)" -ForegroundColor Green
}
catch {
$CSVResults += [PSCustomObject]@{
UserPrincipalName = $_.UserPrincipalName
DisplayName = $_.DisplayName
Department = $_.Department
Status = "Failed: $($_.Exception.Message)"
ProcessedTime = Get-Date
}
Write-Host "✗ Failed: $($_.UserPrincipalName)" -ForegroundColor Red
}
Start-Sleep -Milliseconds 300
}
# Display summary
$SuccessfulCSV = ($CSVResults | Where-Object {$_.Status -eq "Success"}).Count
$FailedCSV = ($CSVResults | Where-Object {$_.Status -ne "Success"}).Count
Write-Host "`nCSV Processing Summary:" -ForegroundColor Cyan
Write-Host " Successful: $SuccessfulCSV" -ForegroundColor Green
Write-Host " Failed: $FailedCSV" -ForegroundColor Red
}
else {
Write-Host "CSV file not found at $CSVPath" -ForegroundColor Red
}
Pro tip: Use CSV processing for incident response scenarios where you need to quickly force password changes for compromised accounts or specific user groups.
Verification: Export CSV results for documentation:
$CSVResults | Export-Csv -Path "C:\Temp\CSV-PasswordReset-Results-$(Get-Date -Format 'yyyyMMdd-HHmmss').csv" -NoTypeInformation