SharePoint Online PowerShell: Resolving error – Connect-SPOService: The remote server returned an error: (400) Bad Request.

Error while executing SharePoint Online PowerShell CMDLET - Connect-SPOService
Error while executing SharePoint Online PowerShell CMDLET - Connect-SPOService

Hi All,

Greetings for the day!

Today new issue and resolution.

Details

  • I need to execute couple of SharePoint Online PowerShell CMDLETs
  • To execute SharePoint Online PowerShell CMDLETs, I have imported SharePoint Online PowerShell module – Microsoft.Online.SharePoint.PowerShell
PS C:\> Import-Module -Name Microsoft.Online.SharePoint.PowerShell
WARNING: The names of some imported commands from the module 'Microsoft.Online.SharePoint.PowerShell' include unapproved verbs that might make them less discoverable. To find the commands with unapproved verbs, run the Import-Module command again with the Verbose parameter. For a list of approved verbs, type Get-Verb.

A screenshot of a PowerShell console showing the command 'Import-Module -Name Microsoft.Online.SharePoint.PowerShell' along with a warning about unapproved verbs in the imported commands.
figure: Import-Module – Importing SharePoint Online PowerShell module
  • SharePoint Online PowerShell module imported successfully.
  • Next step to connect SharePoint online using – Connect-SPOService
  • We were executing Connect-SPOService PowerShell CMDLET as
PS C:\> $cred = Get-Credential

PowerShell credential request
Enter your credentials.
User: prasham@knowledgejunction1.onmicrosoft.com
Password for user prasham@knowledgejunction1.onmicrosoft.com: ***************

PS C:\> Connect-SPOService -Url https://knowledgejunction1-admin.sharepoint.com/ -Credential $cred

  • While executing Connect-SPOService CMDLET, we were getting an error

ERROR / ISSUE

Connect-SPOService: The remote server returned an error: (400) Bad Request.

PowerShell command console displaying a credential request for SharePoint administration, including a user prompt and an error message indicating a '400 Bad Request'.
figure: Error while executing SharePoint Online PowerShell CMDLET – Connect-SPOService

DETAILS

  • For executing SharePoint Online PowerShell CMDLETs, we were using PowerShell 7.5.1
PS C:\> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.5.1
PSEdition                      Core
GitCommitId                    7.5.1
OS                             Microsoft Windows 10.0.22621
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

PowerShell version table output showing version 7.5.1 and other relevant details.
figure: PowerShell version
  • For PowerShell 7 +, we have to use the Windows PowerShell Compatibility feature because the Microsoft.Online.SharePoint.PowerShell module was built with .NET Framework and not .NET Core
  • We have to import the module into PowerShell 7 with an additional switch – UseWindowsPowerShell

SOLUTION

  • Import the SharePoint Online PowerShell module with additional switch – UseWindowsPowerShell as
PS C:\> Import-Module -Name C:\Users\u1086350\Documents\PowerShell\Modules\Microsoft.Online.SharePoint.PowerShell -UseWindowsPowerShell

WARNING: The names of some imported commands from the module 'Microsoft.Online.SharePoint.PowerShell' include unapproved verbs that might make them less discoverable. To find the commands with unapproved verbs, run the Import-Module command again with the Verbose parameter. For a list of approved verbs, type Get-Verb.
WARNING: The names of some imported commands from the module 'Microsoft.Online.SharePoint.PowerShell' include unapproved verbs that might make them less discoverable. To find the commands with unapproved verbs, run the Import-Module command again with the Verbose parameter. For a list of approved verbs, type Get-Verb.
WARNING: The names of some imported commands from the module 'Microsoft.Online.SharePoint.PowerShell' include unapproved verbs that might make them less discoverable. To find the commands with unapproved verbs, run the Import-Module command again with the Verbose parameter. For a list of approved verbs, type Get-Verb.
WARNING: Module Microsoft.Online.SharePoint.PowerShell is loaded in Windows PowerShell using WinPSCompatSession remoting session; please note that all input and output of commands from this module will be deserialized objects. If you want to load this module into PowerShell please use 'Import-Module -SkipEditionCheck' syntax.

Command line output showing the import of the SharePoint Online PowerShell module with the UseWindowsPowerShell switch.
figure: Executing Import-Module CMDLET with “UseWindowsPowerShell” attribute

  • Once we successfully executed Import-Module CMDLET with attribute “UseWindowsPowerShell“, we successfully execute the CMDLET Connect-SPOService CMDLET as
PS C:\> Connect-SPOService -Url https://knowledgejunction1-admin.sharepoint.com/ -Credential $cred

PS C:\> Get-SPOSite

Url                                                                           Owner
---                                                                           -----
https://knowledgejunction1.sharepoint.com/sites/DemoStorageSite_archive       prasham@knowledgejunction1.onmicrosoft.c…
https://knowledgejunction1.sharepoint.com/sites/teamdemo_43_1203
https://knowledgejunction1.sharepoint.com/sites/TestLogoFromSiteDesign        prasham@knowledgejunction1.onmicrosoft.c…
https://knowledgejunction1.sharepoint.com/sites/appcatalog                    prasham@knowledgejunction1.onmicrosoft.c…
PowerShell commands showing the execution of SharePoint Online CMDLETs, including Connect-SPOService and Get-SPOSite, with URLs and owners listed.
figure: Executing Connect-SPOService CMDLET successfully

REFERENCES

Thanks for reading!!!

HAVE A FANTASTIC TIME AHEAD !!! LIFE IS BEAUTIFUL 🙂

Prasham Sabadra

LIFE IS VERY BEAUTIFUL. ENJOY THE WHOLE JOURNEY :) Founder of Microsoft 365 Junction, Speaker, Author, Learner, Developer, Passionate Techie. Certified Professional Workshop Facilitator / Public Speaker. Believe in knowledge sharing. Around 20+ years of total IT experience and 17+ years of experience in SharePoint and Microsoft 365 services Please feel free me to contact for any SharePoint / Microsoft 365 queries. I am also very much interested in behavioral (life changing) sessions like motivational speeches, Success, Goal Setting, About Life, How to live Life etc. My book - Microsoft 365 Power Shell hand book for Administrators and Beginners and 100 Power Shell Interview Questions - https://www.amazon.in/Microsoft-Administrators-Beginners-Interview-Questions/dp/9394901639/ref=tmm_pap_swatch_0?_encoding=UTF8&qid=1679029081&sr=8-11

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.