Many of the posts on this blog and others will require you to connect to Office 365 with PowerShell. The web admin interface for Office 365 is very good, but there are times when PowerShell is more suited. So this post shows you how it is done and the problems you might come up against. I will go through each step in detail and then post complete code.
Connect to Office 365 with PowerShell
- Open a PowerShell session
- Store your Credentials in a variable:
$Cred = Get-Credential
- Enter your Office 365 Credentials when prompted:
- Create a new PowerShell session from the Office 365 Server:
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $Cred -Authentication Basic -AllowRedirection
- Import the session:
Import-PSSession $Session
If you do not receive any errors on this step, continue to step 6. You may receive the error:
Import-PSSession : Files cannot be loaded because running scripts is disabled on this system.
If you do get this error, you need to change your PowerShell Execution Policy.
- Now you can run any commands you need.
- When you have finished, remove the session you created in step 2:
Remove-PSSession $Session
Complete code to connect to Office 365 with PowerShell
$Cred = Get-Credential $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $Cred -Authentication Basic -AllowRedirection Import-PSSession $Session # Run any commands you want here Remove-PSSession $Session
Change PowerShell Execution Policy
If you received this error when you tried to execute step 5:
Import-PSSession : Files cannot be loaded because running scripts is disabled on this system.
Then you need to change your PowerShell execution policy. To connect to Office 365 with Powershell your execution policy to at least Remote signed:
- Open PowerShell as an administrator:
- In your PowerShell window run the command:
Set-ExecutionPolicy RemoteSigned
- Choose “Y” when prompted to change the execution policy.
You will now be able to connect to Office 365 with Powershell.
Jonah says
Vary helpful, thank you very much!!
mainwebsolutions says
Hi there, it seems like it doesn’t work anymore. I tried a few month ago and it worked. but since i tried today it doesent. Can you check that and inform me? The error is happening here:
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $Cred -Authentication Basic -AllowRedirection
WARNUNG: Ihre Verbindung wurde an den folgenden URI umgeleitet:
“https://ps.outlook.com/PowerShell-LiveID?PSVersion=5.1.19041.1645”
New-PSSession : [ps.outlook.com] Beim Verbinden mit dem Remoteserver “ps.outlook.com” ist folgender Fehler
aufgetreten: Zugriff verweigert Weitere Informationen finden Sie im Hilfethema “about_Remote_Troubleshooting”.
In Zeile:1 Zeichen:12
+ $Session = New-PSSession -ConfigurationName Microsoft.Exchange -Conne …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (System.Manageme….RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotin
gTransportException
+ FullyQualifiedErrorId : AccessDenied,PSSessionOpenFailed
I can login to office admin account so the credentials should be right. And the user is also a global admin. Does anyone know a solution?
Matt says
This method doesnt appear to work anymore as there is a ‘Access is Denied’ Error Message.
The users now need to update their EXO module and use the new connect commands:
———————————————>
Install EXO V2 using the following command:
> Install-Module ExchangeOnlineManagement
Once the EXO V2 module is installed you can connect to Exchange Online with MFA using the following command:
> Connect-ExchangeOnline