How to Automate Connect-SPOService Without a PowerShell Sign-In Prompt
Question details
The user wants to automate the Connect-SPOService command in a PowerShell script without triggering an interactive sign-in prompt, but encounters credential errors.

- Product
- SharePoint Online Management Shell
- Device & OS
- not provided
- Scenario
- Running automated PowerShell scripts for SharePoint Online administration.
- Observed behavior
- Connect-SPOService works interactively but fails in a script with sign-in or password errors despite having MFA disabled.
Ensure you have the latest version of the SharePoint Online Management Shell installed and verify that your account is assigned the SharePoint Administrator or Global Administrator role.
Pass a PSCredential Object for Silent Authentication
Use this method to securely format and pass credentials within a script when Multi-Factor Authentication (MFA) is fully disabled.
When automating SharePoint Online connections, the -Credential parameter requires a properly formatted PSCredential object rather than a plain-text password. Passing plain text will cause the authentication to fail.
In your PowerShell script, convert your plain-text password to a secure string using the command: $securePassword = ConvertTo-SecureString -String 'YourPassword' -AsPlainText -Force.
Combine your admin username and the secure string password by executing: $credentials = New-Object System.Management.Automation.PSCredential($username, $securePassword).
Execute the connection command using the created object: Connect-SPOService -Url 'https://yourdomain-admin.sharepoint.com' -Credential $credentials.

Verify Tenant Modern Authentication Settings
Check tenant-level settings if the PSCredential object still fails, as legacy authentication might be blocked.
Boost Your Productivity with WPS Office
While troubleshooting advanced Microsoft 365 PowerShell scripts, you might need a lightweight, fast, and highly compatible office suite for your daily documentation. WPS Office offers a familiar interface and seamless compatibility with Microsoft Word, Excel, and PowerPoint files without the heavy subscription fees.

Frequently Asked Questions
Why does Connect-SPOService work interactively but fail in my script?
Interactive logins natively support modern authentication and conditional access policies. Scripts using the -Credential parameter rely on legacy basic authentication, which fails if Multi-Factor Authentication (MFA) or modern security defaults are enabled on the tenant.
How do I securely automate SharePoint Online logins if MFA is enforced?
If MFA is required, you cannot use a username and password in a script. Instead, you need to set up an Azure AD App Registration and use certificate-based authentication. Using Connect-PnPOnline from the PnP PowerShell module is the recommended approach for this scenario.
What format does the Connect-SPOService credential parameter expect?
The -Credential parameter strictly requires a PSCredential object. Passing a standard string for the password will result in a sign-in name or password error. You must convert the string to a SecureString before creating the PSCredential object.




