r/PowerShell • u/Ok_Dependent9976 • May 08 '26
Solved Creating new O365 users using PowerShell with MFA Enforced
Hi All, I'm trying to develop an application (.net front end running PowerShell commands) that uses Cert authentication to allow easy action of repeated mundane actions across multiple tenants without having to interactively login each time.
Everything is working fine, except part of my "new user" work flow.
New users need to be set up with authentication numbers pre-registered, which I can do. (I know using phone numbers is not as secure as the Authenticator app)
But, what I can’t get to work is enforcing MFA for the user, because Connect-MsolService doesn’t support certificate login, (and is due to be depreciated soon I believe), and prompting for an interactive login kinda defeats the point!
I was hoping having security defaults enabled would result in MFA being enforced for all users, but it seems that if the user has an authentication method setup (E.g. phone numbers), then the wizard doesn’t trigger on first login and MFA is not enforced. The only way I've found round it so far is logging into the portal and using the legacy per-user MFA to set MFA to enforced there, which, again defeats the point!
We don't have the licences to use conditional access policies.
Can anyone suggest anything?
13
u/Creddahornis May 08 '26
If I've not misunderstood your message, could you not set the 'perUserMfaState' to 'enforced' using Graph Powershell?
You'll need to set up certificate based authentication into Graph too, if you want the tool to run without interactive login, but I assume you've done this if you've automated some of this already 🙂
Update authentication method states - Microsoft Graph beta | Microsoft Learn
4
u/Ok_Dependent9976 May 08 '26
I ended up getting it to work with Microsoft.Entra.Beta.SignIns
Update-EntraBetaUserAuthenticationRequirement -UserId $UserID -PerUserMfaState 'enabled'
Thank you to everyone who commented!
6
u/Fallingdamage May 08 '26
Switch to graph powershell module and web requests. Classic modules are going to be less and less supported. Best to move along now.
I made the switch and life has been a lot easier managing AppID permissions with cert auth.
3
u/KavyaJune May 08 '26
You can setup per-user MFA via MS Graph.
Need to replace the actual UserId in the URI.
2
u/BlackV May 08 '26 edited May 08 '26
Connect msol? Is that not very old and deprecated
2
u/zac_goose May 09 '26
May I suggest that you look at CIPP, it’s a multi tenant M365 management platform
1
u/chaosphere_mk May 08 '26
MSOnline is deprecated and you should switch to the graph module ASAP
1
u/jr49 May 09 '26
Looks like it’s supported in Graph but in the beta endpoint. I prefer making my own rest API calls but have come around to using the mgraph modules for authentication while still using invoke-mggraphrequest rather than learning all the random commands.
https://learn.microsoft.com/en-us/entra/identity/authentication/howto-mfa-userstates#use-microsoft-graph-to-manage-per-user-mfa
0
u/Fatel28 May 08 '26
You should be using conditional access
1
u/[deleted] May 15 '26
[removed] — view removed comment