SharePoint Online PowerShell – Connect-SPOService – resolving error “The remote server returned an error: (400) Bad Request” while executing in PowerShell 7.4

PowerShell - Error while executing Connect-SPOService PowerShell CMDLET in PowerShell 7.4
PowerShell - Error while executing Connect-SPOService PowerShell CMDLET in PowerShell 7.4

Hi All,

Greetings for the day!!!

Today new issue and solution 🙂

Today, resolving error “The remote server returned an error: (400) Bad Request.” while executing the SharePoint Online PowerShell “Connect-SPOService” in PowerShell 7.4

Details

  • We need to fetch the properties of one of the SiteCollection using PowerShell.
  • So we are using “SharePoint Online PowerShell“.
  • We are connecting to Microsoft 365 using “Connect-SPOService” PowerShell CMDLET.
  • While executing “Connect-SPOService” PowerShell CMDLET, we are getting an error “Bad Request“.
  • We are using PowerShell 7.4
PowerShell 7.4.6
fig: PowerShell 7.4.6

Issue

PS C:> Connect-SPOService -Url https://knowledgejunction1-admin.sharepoint.com/
Connect-SPOService: The remote server returned an error: (400) Bad Request.

PowerShell - Error while executing Connect-SPOService PowerShell CMDLET in PowerShell 7.4
fig: PowerShell 7.4.6 – Error while executing Connect-SPOService PowerShell CMDLET in PowerShell 7.4

Cause / Reason

  • Microsoft.Online.SharePoint.PowerShell module was built with .NET Framework and not .NET Core.
  • For PowerShell 7, we have to use the Windows PowerShell Compatibility feature
  • So we have to import the module into PowerShell 7 with an additional switch -UseWindowsPowerShell

Solution

  • Execute the import module with an additional switch -UseWindowsPowerShell
PS C:\> Import-Module 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.

PowerShell - executing Import-Module PowerShell CMDLET with -UseWindowsPowerShell attribute
PowerShell 7.4.6 – executing Import-Module PowerShell CMDLET with -UseWindowsPowerShell attribute
  • As we executed Import-Module CMDLET with -UseWindowsPowerShell attribute, we could execute the Connect-SPOService and other SharePoint Online PowerShell CMDLETs
PS C:\> Connect-SPOService -Url https://knowledgejunction1-admin.sharepoint.com/
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…
https://knowledgejunction1.sharepoint.com/sites/LearningPortal                prasham@knowledgejunction1.onmicrosoft.c…
https://knowledgejunction1.sharepoint.com/sites/spswapdemo

PowerShell 7.4 - Connect-SPOService
PowerShell 7.4.6 – Connect-SPOService CMDLET executed successfully.

Thanks for reading. HAPPY LEARNING !

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.