Connect to Office 365 via PowerShell

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

5 Replies to “Connect to Office 365 via PowerShell”

Leave a Reply

Your email address will not be published. Required fields are marked *