How to Authenticate MFA in a Scheduled PowerShell Task
Question details
The user needs to authenticate to Azure via PowerShell in a scheduled task, but interactive MFA prompts prevent the script from running successfully.

- Product
- PowerShell / Azure
- Device & OS
- not provided
- Scenario
- Running automated PowerShell scripts for Azure management via Windows Task Scheduler.
- Observed behavior
- The scheduled task fails or hangs indefinitely because it cannot display the interactive login prompt required for Multi-Factor Authentication (MFA).
Ensure you have appropriate administrative privileges in your Azure Active Directory (Entra ID) to create App Registrations or assign Managed Identities.
Authenticate Using an Azure Service Principal
The standard approach for unattended scripts is to use a Service Principal with a client secret or certificate instead of user credentials.
Scheduled tasks run in a non-interactive session. Any script that requires user interaction, such as entering an MFA code, will indefinitely hang or fail. Service Principals act as an identity for your application or script, allowing secure, non-interactive authentication.
Navigate to the Azure Portal, open Microsoft Entra ID (Azure AD), and select 'App registrations'. Click 'New registration' and provide a descriptive name for your automation script.
Under the registered app, go to 'Certificates & secrets' and create a new client secret (or upload a certificate). Copy the secret value immediately, as it will be hidden after you leave the page.
Grant the App Registration the necessary role-based access control (RBAC) permissions in your Azure subscription to manage the specific resources your script targets.
Modify your script to use the Service Principal for connection. Use the command format: Connect-AzAccount -ServicePrincipal -ApplicationId <AppID> -Tenant <TenantID> -CertificateThumbprint <Thumbprint> (or use the client secret credential object).

Utilize System-Assigned Managed Identities
If your scheduled task runs on an Azure Virtual Machine, enabling a Managed Identity is the most secure and easiest authentication method.
Simplify Your Workflow with WPS Office
While you configure advanced automation and PowerShell scripts in the Microsoft ecosystem, consider standardizing your daily document tasks with WPS Office. It provides a lightweight, highly compatible, and free alternative to Microsoft Office for all your word processing, spreadsheet, and presentation needs.
- 1. Download the Installer: Visit the official WPS Office website and download the free installation package for your operating system.
- 2. Install WPS Office: Run the installer and follow the on-screen instructions to set up the software in just a few clicks.
- 3. Open Your Documents: Launch WPS Office and instantly open your existing Microsoft Office files without losing any formatting.

Frequently Asked Questions
Can I bypass MFA for a specific user account to run scheduled tasks?
While it is technically possible to use Conditional Access policies to bypass MFA for specific trusted IPs or accounts, Microsoft strongly discourages this due to severe security risks. Using Service Principals or Managed Identities is the recommended and secure solution.
Why does my PowerShell script work manually but fail in Task Scheduler?
When run manually, you are operating in an interactive session and can easily respond to MFA prompts in a pop-up window. Task Scheduler runs scripts non-interactively in the background, causing the script to hang indefinitely when it waits for user MFA input.
How do I securely store the Service Principal credentials for my scheduled script?
Avoid hardcoding client secrets in your plaintext script. Instead, use the Windows Credential Manager, the Export-Clixml cmdlet to securely store encrypted credentials on the disk restricted to the running service account, or leverage Azure Key Vault.
Does the Connect-MsolService cmdlet support Service Principal authentication?
The older MSOnline module (Connect-MsolService) has limited support for modern authentication flows. It is highly recommended to migrate your administration scripts to the Microsoft Graph PowerShell SDK, which natively and fully supports Service Principals and App-only authentication.




