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

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.

- 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.

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

- 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 –
UseWindowsPowerShellas
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.

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…

REFERENCES
- PowerShell – Exploring Module CMDLETs – https://knowledge-junction.in/2022/12/12/__trashed-2/
- about_Windows_PowerShell_Compatibility – https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_windows_powershell_compatibility?view=powershell-7.5&viewFallbackFrom=powershell-7.2&WT.mc_id=DT-MVP-5003535
Thanks for reading!!!
HAVE A FANTASTIC TIME AHEAD !!! LIFE IS BEAUTIFUL 🙂

You must be logged in to post a comment.