I have posted on this before but Microsoft has updated the method so I’ll update on here too. To connect your Office 365 instance to PowerShell the process is outlined here. When trying this you may get the error ‘The specified module ‘MSOnline’ was not loaded because no valid module file was found in any module directory.’ The below steps show you how to overcome this. Using the below commands you will also be able to connect to Exchange Online using PowerShell.
1. Install the .net framework 3.5 – you can do this through Programs and Features>Windows Components but if this fails use this method.
2. Open PowerShell as an administrator and run the below commands (these may not work if you are behind a proxy):
Set-ExecutionPolicy -ExecutionPolicy Unrestricted
Register-PSRepository -Default
Get-PSRepository
Install-Module MSOnline
$UserCredential = Get-Credential Connect-MsolService -Credential $UserCredential
$exchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential $UserCredential -Authentication "Basic" -AllowRedirection
Import-PSSession $exchangeSession -DisableNameChecking
3. To test your connection to Office 365 run the command:
Get-MsolDomain
4. If you are behind a proxy server and you cannot disable it you can run the below to authenticate and go through it:
$wc = New-Object System.Net.WebClient $wc.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
Thank you!
This is awesome. THANK YOU!!!!!
Wow! thank you indeed for documenting this. This did the trick
Finally. That took forever. The missing command was Get-PSRepository
If you want to connect all Office 365 services with a single cmdlet, you can use this script.
https://o365reports.com/2019/10/05/connect-all-office-365-services-powershell/
The script supports both MFA and non-MFA accounts.