Force user to change password on next login.

John Davies
John Davies used Ask the Experts™
on
What I am looking for is some help on the following script but for it to read a CSV file instead of individual

Set-MsolUserPassword -UserPrincipalName user@domain.com -ForceChangePasswordOnly $true -ForceChangePassword $true

The reason i am after this is so I can force groups of people rather than all.

I am in a CLOUD OFFICE 365 environment with no AD or Hybrid.

Thanks

John
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
AlexSenior Infrastructure Analyst

Commented:
$Users = Get-content C:\temp\source.txt
Foreach ($User in $Users) { Set-MsolUserPassword -UserPrincipalName $User -ForceChangePasswordOnly $true -ForceChangePassword $true}

Open in new window


That should do it, you'll have to have the user@domain.com in the text file

If you really want to use a csv

$Users = import-csv C:\temp\source.csv
Foreach ($User in $Users) { Set-MsolUserPassword -UserPrincipalName $User -ForceChangePasswordOnly $true -ForceChangePassword $true}

Open in new window

Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
Assuming you have a blabla.csv with a column UPN to designate the users:

Improt-CSV blabla.csv | % { Set-MsolUserPassword -UserPrincipalName $_.UPN -ForceChangePasswordOnly $true -ForceChangePassword $true }

Open in new window

Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
All you need is a loop over the file contents.
Flat file, one UPN per line:
Get-Content -Path "C:\Temp\userlist.txt" | ForEach-Object {
	Set-MsolUserPassword -UserPrincipalName $_ -ForceChangePasswordOnly $true -ForceChangePassword $true
}

Open in new window

Csv with a column 'UPN':
Import-Csv -Path "C:\Temp\userlist.txt" | ForEach-Object {
	Set-MsolUserPassword -UserPrincipalName $_.UPN -ForceChangePasswordOnly $true -ForceChangePassword $true
}

Open in new window

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

AlexSenior Infrastructure Analyst

Commented:
I BEAT oBdA!!!!!!

and our code is pretty much identical, I feel pride, immense amounts of pride :D

I'm not a shadow on him though... Or her, you never know.

Author

Commented:
Lol Alex,

Just run the code to a CSV that has one column UserPrincipalName and get the following error, what I was getting before :-(

Set-MsolUserPassword : Cannot bind argument to parameter 'UserPrincipalName' because it is null.
At C:\Users\John\Desktop\Passwords\Change2016.ps1:2 char:42
+     Set-MsolUserPassword -UserPrincipalName $_.UPN -ForceChangePasswo ...
+                                             ~~~~~~
    + CategoryInfo          : InvalidData: (:) [Set-MsolUserPassword], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Online.Administration.Automation.SetUserPa
   ssword
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
If the column is named UserPrincipalName, then you have to change the loop variable's ($_) property accordingly:
Import-Csv -Path "C:\Temp\userlist.txt" | ForEach-Object {
	Set-MsolUserPassword -UserPrincipalName $_.UserPrincipalName -ForceChangePasswordOnly $true -ForceChangePassword $true
} 

Open in new window

Author

Commented:
Bingo, that was it.

Thank you all for your help, really really appreciated!

Have a great day guys!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial