Skip to content

Instantly share code, notes, and snippets.

@jrgilman
Created January 6, 2021 22:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrgilman/36f7b53fc3037df46f5f8e28adf4c6f9 to your computer and use it in GitHub Desktop.
Save jrgilman/36f7b53fc3037df46f5f8e28adf4c6f9 to your computer and use it in GitHub Desktop.
Resets all user passwords (except the user you are logged in as) in Office 365 via PowerShell and outputs them to a csv
Import-Module MSOnline
Add-Type -AssemblyName 'System.Web'
$creds = Get-Credential
Connect-MsolService -Credential $creds
$ignoreUser = Get-MsolUser -UserPrincipalName $creds.Username
if ($ignoreUser -eq $null) {
Write-Host "Could not find the user you logged in as!"
exit 4
}
$AllResetUsers = @()
Get-MsolUser | ForEach-Object {
if ( $_.ObjectId -ne $ignoreUser.ObjectId) {
Write-Host $_.UserPrincipalName
$newPassword = Set-MsolUserPassword -UserPrincipalName $_.UserPrincipalName -ForceChangePassword $true
$AllResetUsers += [pscustomobject]@{
Email = $_.UserPrincipalName
First = $_.FirstName
Last = $_.LastName
NewPassword = $newPassword
}
}
}
if ( Test-Path "outfile.csv" ) {
Remove-Item "outfile.csv"
}
$AllResetUsers | Export-Csv -Path "outfile.csv" -NoTypeInformation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment