Created
January 6, 2021 22:56
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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