EOP / Office 365: Connect and Use Powershell
The blog will explain the steps on how to connect to office 365 or Exchange online protection for the management using powershell of your desktop client computer.
Connecting Steps:
-
Open powershell with Run as Administrator on your computer
-
Run the command to cache authentication
$Cred = Get-credential
This will popup authentication windows. Type your login id which is the User principal name and password.
-
Run the command to configure the powershell to use the office 365.
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $Cred -Authentication Basic –AllowRedirection
-
Verify and change the execution policy.
To verify run the command:
Get-Executionpolicy
To change run the cmd
Set-ExecutionPolicy RemoteSigned
Import-PSSession $Session
If your executionpolicy will be Restricted then you will see the below error:
Import-PSSession : Files cannot be loaded because the running of scripts is disabled on this system. Please provide a
valid certificate with which to sign the files.
As an example I ran the below command
Get-Mailbox
Disconnecting Session
Run the below command to disconnect the session:
Remove-PSSession $Session
To verify run the working command as mentioned below and you will see the below error
Command: Get-Mailbox
Error: The term ‘Get-Mailbox’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
Prabhat Nigam
Microsoft MVP | Exchange Server
Team@MSExchangeGuru
February 17th, 2014 at 8:51 am
[…] EOP / Office 365: Connect and Use Powershell […]
August 30th, 2014 at 4:01 pm
Thanks 🙂
This was of great help to me
October 10th, 2014 at 10:54 am
Many thanks for the great post, I was searching for details like this, going to check out the other blog posts.
March 30th, 2015 at 4:39 pm
Thanks a lot. Great help!!
June 19th, 2015 at 6:32 am
[…] https://msexchangeguru.com/2014/02/03/eop-o365-connect-powershell/ […]
June 22nd, 2015 at 2:57 am
[…] Connect to the Office 365 PowerShell using this blog – https://msexchangeguru.com/2014/02/03/eop-o365-connect-powershell/ […]
June 30th, 2015 at 5:49 pm
[…] Connect to the Office 365 tenant using remote Windows PowerShell. For step-by-step connection instructions from here – https://msexchangeguru.com/2014/02/03/eop-o365-connect-powershell/ […]
August 19th, 2015 at 5:41 am
Hi,
I have successfully connect to Office 365 using Windows Azure Active Directory Module for Windows PowerShell. I can also run many commands, such as Get-MsolUser. However I still have problem that the power shell not recognize the commands “Set-TransportConfig -AddressBookRoutingEnabled $true”
I would appreciate if any one can advise how to resolve this issue?
Thank you
August 19th, 2015 at 12:08 pm
You need to connect to exchange module in other words office 365 module. Azure module is different.
October 6th, 2015 at 6:20 am
Great work I have been going around in circles to do this
January 23rd, 2016 at 1:39 am
[…] Now you will be wondering just for one mailbox whole batch will be waiting. Answer is yes if we wish to run a command for the batch but if we decide to run a command for other mailboxes which are in the auto-suspend status after 95% sync then we have to use PowerShell. Use this blog to connect to the office 365 powershell […]
August 28th, 2017 at 6:57 am
Can we follow the same steps to connect to Office 365 through powershell even without hybrid model. As I am unable to connect to office 365 through power-shell mode with E3 plan
August 28th, 2017 at 11:56 pm
Hi Guru,
Please share the error
August 29th, 2017 at 10:06 am
Thanks for your response .
Please find the below error message
New-PSSession : [outlook.office365.com] Connecting to remote server
outlook.office365.com failed with the following error message : [ClientAccessSe
rver=MA1PR01CA0100,BackEndServer=,RequestId=d6aef337-8c1c-4fe9-aa72-e827b680279
b,TimeStamp=8/29/2017 1:59:55 PM] [FailureCategory=LiveID-InvalidCreds] For
more information, see the about_Remote_Troubleshooting Help topic.
At line:1 char:12
+ $Session = New-PSSession -ConfigurationName Microsoft.Exchange
-ConnectionUri ht …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~
+ CategoryInfo : OpenError: (System.Manageme….RemoteRunspace:Re
moteRunspace) [New-PSSession], PSRemotingTransportException
+ FullyQualifiedErrorId : -2144108477,PSSessionOpenFailed
PS C:\Windows\system32>
August 30th, 2017 at 2:29 am
Which command you had run. This one or something else.
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $Cred -Authentication Basic –AllowRedirection