Connect to Azure AD
Azure AD PowerShell Modunle
You need install MSOnline module only once. In the next time, run to connect to Azure AD PowerShell.
##Connect to MSOnline
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine -Force;
Install-PackageProvider -Name NuGet -Force;
Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted;
Install-Module -Name MSOnline;
Connect-MsolService;
![](https://leoguides.com/wp-content/uploads/2022/04/2022-04-05_114406.png)
Reset password for multiple users
ForceChangePassword
By default, users must change their password in the first time sign in, add this to to ignore first time password change.
##Reset password for multiple users
$NewPassword = 'LeoGuides@2022'
Import-Csv "C:\Temp\Users.csv" |
ForEach {Set-MsolUserPassword –UserPrincipalName $_.UserPrincipalName -NewPassword $NewPassword}
Reset password for multiple users and generate random password:
$Users = Import-Csv "C:\Temp\Users.csv"
$Results =
foreach($Users in $Users){
$RandomPassword = ""
$RandomPassword += ([char[]]"ABCDEFGHIJKLMNOPQRSTUVWXYZ" | Get-Random)
$RandomPassword += $(1..3 | % { [char[]]"abcdefghijklmnopqrstuvwxyz" | Get-Random }) -join ""
$RandomPassword += $(1..4 | % { [char[]]"0123456789" | Get-Random }) -join ""
$set = @{
UserPrincipalName = $Users.UserPrincipalName
NewPassword = $RandomPassword
ForceChangePassword = $true
}
try{
$SetNewPW = Set-MsolUserPassword @set -ErrorAction Stop
[pscustomobject]@{
UserPrincipalName = $Users.UserPrincipalName
NewPassword = $RandomPassword
Status = "Password Set Successfully"
}
}
catch{
[pscustomobject]@{
UserPrincipalName = $Users.UserPrincipalName
NewPassword = $RandomPassword
Status = $error[0].exception.message
}
}
}
$Results | Export-Csv -Path "C:\Temp\Results.csv" -NoTypeInformation | Start "C:\Temp\Results.csv"
5/5 - (1 vote)