Windows – Unable to Connect to Office 365 via Powershell on Windows 10

office365powershellwindows 10

I'm trying to connect to Office 365 via Powershell just like I always have and for whatever reason it is not working.

I'm running Windows 10 Enterprise and one of the things I noticed is that Microsoft Online Service Sign-In Assistant for IT Professionals is not available for Windows 10. This makes me think perhaps it is built into Windows 10 by default but it's not working so perhaps that isn't the case either.

Below are the steps I take to try and connect to O365 in Powershell:

$Credential = Get-Credential
Import-Module MsOnline
Connect-MsolService -Credential $credential
$ExchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential $credential -Authentication "Basic" -AllowRedirection
Import-PSSession $ExchangeSession

I also tried this without success:

$Credential = Get-Credential
Import-Module MsOnline
Connect-MsolService -Credential $credential
$ExchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Cred -Authentication Basic -AllowRedirection #PS 5.0+
Import-PSSession $ExchangeSession

The error I get is this:

Connect-MsolService : The type initializer for 'Microsoft.Online.Administration.Automation.ConnectMsolService' threw
an exception.
At C:\Users\xxxxx\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:3 char:1
+ Connect-MsolService -Credential $credential
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [Connect-MsolService], TypeInitializationException
    + FullyQualifiedErrorId : System.TypeInitializationException,Microsoft.Online.Administration.Automation.ConnectMso
   lService

I ran the command below and found I was using Windows Azure AD Module v1.0.8262.2

PS C:\Users\xxxxx> (get-item C:\Windows\System32\WindowsPowerShell\v1.0\Modules\MSOnline\Microsoft.Online.Administrati
on.Automation.PSModule.dll).VersionInfo.FileVersion
1.0.8262.2

I've done plenty of Googling but haven't found anything that gets this working for me. I think this should be on SuperUser vs. ServerFault since this is an issue with my laptop but if you think it is better suited for ServerFault feel free to move this.

Thanks in advance for any assistance!

Best Answer

The Microsoft Online Sign in assistant is required and usually installed prior to installing the Azure AD module the first time. If you already have O365 deployment - you may have just had it installed with those applications (O365 pro plus, visio, OneDrive sync, etc).

While I know there are some newer versions - for working with Azure AD I believe it's still recommended to use the RTW version of the assistant (and it's the version I install when working it). newer versions of the tool may give you problems with the Azure AD module.

https://www.bing.com/search?q=Microsoft+Online+Services+Sign-In+Assistant+for+IT+Professionals+RTW+&pc=MOZI&form=MOZSBR

**Edit ** Azure AD PowerShell V1 is still supported but Microsoft is porting commands over to V2. The commands are not inter-operable, as V2 uses the Microsoft Graph (different API). Commands use AzureAD instead of MSOL.

If your running WMF 5.0 you can install the modules directly with one of the following:

Install-Module AzureAD
Install-Module AzureADPreview

These can both be installed side by side, which I recommend as some commands in the preview module are required for certain operations and do not exist in the GA version.

These modules support modern auth and MFA and once loaded it's easy to connect with Connect-AzureAD command that will open a web window.