SharePoint Online PowerShell – Connect-SPOService – resolving error “The remote server returned an error: (400) Bad Request” while executing 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

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

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.

-UseWindowsPowerShell attribute- As we executed Import-Module CMDLET with
-UseWindowsPowerShellattribute, 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

Thanks for reading. HAPPY LEARNING !

You must be logged in to post a comment.