Back to script samples page
Setting passwords for multiple users using CSV file

We have demonstrated how to bulk set the password for a group of users.
 
A bulk password change can be useful to change generic logins on a regular basis. By designating a group of users by a department you can bulk reset passwords based on that department name.
 
In this example we will reset all users in the “Volunteers” department, but to do this we need to export the Office 365 usernames to a CSV file. It is a good idea to review the names contained in the CSV file to ensure you have identified the correct users before resetting any passwords.

Once we have a validated list of users in CSV format, we have three simple methods to create and assign a new password to each user, depending on your use case.

Note: We have also included a command to force a password reset upon login but this is optional.

Note: Once you have a list of users in a CSV file, below are three ways you can configure a new password for each.

# Setting passwords for multiple users using CSV file.

# This first command will import the Azure Active Directory module into your PowerShell session.
Import-Module MSOnline

# Capture administrative credential for future connections.
$credential = get-credential

# Establishes Online Services connection to Azure Active Directory  
Connect-MsolService -Credential $credential

# This command will export a list of all users in the “Volunteers” department to a CSV file. It is a good idea to review the names contained in the CSV file to ensure you have identified the correct users.
Get-MsolUser | Where { $_.department -eq "Volunteers" } | select UserPrincipalName | Export-Csv .\volunteers.csv -NoTypeInformation

# Once you have a list of users in a CSV file, here are three ways you can configure a new password for each.
#   1. Set a single password and assign the same password to all users
#   2. Manually define a new password for each user and assign in bulk.
#   3. Have Azure AD automatically generate a password for each user, and assign in bulk.

# OPTION 1 - Assign a password to a variable and assign to all users in the CSV file 
# Set new password (note that it must comply with the password policy of 9-16 characters, with at least one number or special character)
$PASS = 'New@PassWord'
# Read the CSV file; reset the passwords for users contained in the file and force the user to change their password at next login.  
Import-Csv .\Volunteers.csv | % {Set-MsolUserPassword -userPrincipalName $_.UserPrincipalName -NewPassword $PASS -ForceChangePassword $True}

# OPTION 2 - Populate an additional column in the CSV called 'Password' with valid passwords, and assign to all users (and enforce password change). 
Import-Csv .\Volunteers.csv | % {Set-MsolUserPassword -userPrincipalName $_.UserPrincipalName -NewPassword $_.Password -ForceChangePassword $True}

# OPTION 3 - Have Azure AD automatically generate a random password for each user, and assign. The command will return a list of UPN's and their new password once complete.
Import-Csv .\Volunteers.csv | % {[string[]]$a+= $_.UserPrincipalName + "  " + (Set-MsolUserPassword -userPrincipalName $_.UserPrincipalName -ForceChangePassword $True)} ;$a
Downloaded 0 times
* * * * * (1)
Comments

No Comments