Forum Discussion

Richard_Eagles's avatar
Richard_Eagles
Copper Contributor
Jun 07, 2022
Solved

Connect-SPOService not working in PowerShell 7

Hi all,

 

I'm having some issues getting Connect-SPOService working in PowerShell 7 (7.2.4).  It works fine in Windows PowerShell (5.1.22), however it will always generate errors when trying to us it in PS 7 which I've listed below.

 

Firstly if I open Windows PS, I can use it straight away, however if I open PS 7, I have to import the module in order to use it.  I guess I can fix this with a profile adjustment, but is there a reason why this would be the case? 

 

Secondly even when it's been imported, if I try to connect with the following, I get an error:

 

Connect-SPOService -Url https://***-admin.sharepoint.com
Connect-SPOService: No valid OAuth 2.0 authentication session exists

 

Never seen this before in Windows PS and I'm not sure how to resolve it so I tried connecting with this instead.  Still got an error but a different one this time:

 

Connect-SPOService -Url https://***-admin.sharepoint.com -Credential ***@***.com
Connect-SPOService: The sign-in name or password does not match one in the Microsoft account system.

I know these are the right credientials as again they work fine in Windows PS. 

 

This lead me to think that something still wasn't being imported correctly into PS 7, so I had a look at the modes, and I noticed that the ExportedCommands don't appear when Get-Module is run in PS 7, but again they do in Windows PS.

 

 

I'm guessing this could be part of the issue but I'm not sure how to resolve it.  From what I can see everything appears fine, but I'm sure I'm missing something here.  I've tried setting my ExecutionPolicy to unrestricted in case that was the problem, however it didn't appear to change anything.

If anyone has seen this before or could provide any help it would be greatly appreciated.  I realise that I could just use Windows PS, but it feels like PS 7 is the way forward and it would be nice to better understand why this is happening.

 

Many thanks in advance. 

9 Replies

  • helloDavid's avatar
    helloDavid
    Copper Contributor
    I'm still having trouble with this in 2024 I don't know why PS 7.4 hasn't been updated to support these commands. I finally found that if you force PS 5.1 then all the Connect-SPO commands work because they're supported in that version. I forced it by using Powershell, which launches in 7.4.3 for me, then running "powershell -version 5.1". I can manage SharePoint Online using Powershell now.
  • Richard_Eagles's avatar
    Richard_Eagles
    Copper Contributor
    Thanks both for your replies. This makes me feel better that I'm not missing anything. 🙂 It feels like it's not there at the moment so I think I might stick to Windows PS for SharePoint work for the moment.
    I did try the Import-Module with the -UseWindowsPowerShell parameter and this did appear to work. Managed to login fine with no errors so this looks like a useable workaround if needed. 👍
    Cheers again both for your help. Really appreciate it.
  • You can try using the Import-Module microsoft.online.sharepoint.powershell -UseWindowsPowerShell option to open it in PowerShell 7 in a PowerShell 5 way

Resources