{"id":1196,"date":"2021-06-24T02:24:22","date_gmt":"2021-06-24T02:24:22","guid":{"rendered":"https:\/\/techpog.com\/?p=1196"},"modified":"2022-10-12T17:41:39","modified_gmt":"2022-10-12T17:41:39","slug":"how-to-connect-to-powershell-to-your-office-365-exchange-account","status":"publish","type":"post","link":"https:\/\/techpog.com\/microsoft\/office-365\/how-to-connect-to-powershell-to-your-office-365-exchange-account\/","title":{"rendered":"How to Connect to PowerShell to your Office 365 Exchange Account"},"content":{"rendered":"
Managing Exchange Online with PowerShell makes a lot of daily tasks a lot easier (and quicker). Not only for your daily tasks you may need PowerShell, but some settings in Exchange Online can only be changed with PowerShell. So how do you connect to Exchange Online with PowerShell?<\/span><\/p>\n
\n
\nThe latest version of\u00a0Exchange Online Powershell Module, EXO V2,\u00a0<\/strong>that we are going to use supports modern authentication and will work with MFA. So you don\u2019t need to create an app password anymore.<\/p>\n
Requirements for EXO V2<\/h2>\n
The new Exchange Online PowerShell module only works on PowerShell 5.x and lower. It doesn\u2019t work on Linux or Mac. Support for PowerShell 6 and 7 is planned, but there is no release date announced yet.<\/p>\n
You will need to configure PowerShell to run remote scripts. By default this is disabled.<\/p>\n<\/div>\n
\n
Open PowerShell in an elevated mode<\/strong> \nPress Windows key + X and choose Windows PowerShell (admin)<\/li>\n
Set the execution policy<\/strong>\u00a0to Remote Signed:<\/li>\n<\/ol>\n
You only need to set this once per computer. If haven\u2019t set the execution policy and try to connect to Exchange Online you will get an error:<\/p>\n
Files cannot be loaded because running scripts is disabled on this system. Provide a valid certificate with which to sign the files.<\/em><\/p>\n
Install the Exchange Online V2 Module in PowerShell<\/h3>\n
We need to install the EXO V2 Module in PowerShell before we can connect to Exchange Online. Again open an Elevated Windows PowerShell window:<\/p>\n
\n
Open PowerShell in an elevated mode<\/strong> \nPress Windows key + X and choose Windows PowerShell (admin)<\/li>\n
Install PowerShellGet<\/strong> \nWe need to install PowerShellGet before we can install the EXO V2 Module.<\/li>\n<\/ol>\n
\n
\n
\n
# Close and re-open your PowerShell window when done<\/span><\/div>\n<\/div>\n
Automatically check if EXO Module is installed<\/h3>\n
Are you going to use the Exchange Online module in a script? Then make sure you automatically check if the module is installed before your try to connect.<\/p>\n
With the use of a single cmdlet, we can list all installed modules in PowerShell. You can prevent unnecessary errors by simply verifying that the ExchangeOnlineManagement module is available.<\/p>\n
The cmd above should return a list of installed Exchange Online modules. If the result is empty, then we know that the module isn\u2019t installed.<\/p>\n
Connect to Exchange Online with PowerShell<\/h2>\n
With the Exchange Online Module installed we can now easily connect to Exchange Online with a single cmd in PowerShell:<\/p>\n
The new EXO V2 module also supports connecting to another tenant. If you are a Microsoft Partner and need to connect to another tenant then you can connect to it by adding the\u00a0DelgatedOrganization\u00a0<\/strong>switch.<\/p>\n
It\u2019s a good idea to check if a connection already exists when using the Exchange Online module in scripts. Keep in mind that you can only have 5 simultaneous connections to Exchange Online. And you probably don\u2019t want to log in if you don\u2019t need to.<\/p>\n