Connect MFA PowerShell using PowerShell ISE

How to Connect MFA PowerShell in PowerShell ISE?

If you are aware of MFA accounts and connecting to PowerShell, then scroll to the bottom for the 3 methods of connecting to an MFA enabled account to Exchange Online PowerShell (Office 365)

Multi-factor authentication (MFA) is a method of authentication that requires the use of more than one verification method and adds a second layer of security to user sign-ins and transactions.

Office 365 uses multi-factor authentication to help provide the extra security and is managed from the Office 365 admin center.

Reference: Plan for multi-factor authentication for Office 365 Deployments: https://docs.microsoft.com/en-us/office365/admin/security-and-compliance/multi-factor-authentication-plan?view=o365-worldwide

MFA is enabled per user. This means that if a user has MFA-enabled, they won’t be able to use a non-browser client, such as Outlook 2013 with Office 365, until they create an app password. An app password is a password that is created within the Azure portal and that allows the user to bypass MFA and continue to use their application.

Reference: Set up multi-factor authentication for Office 365 users: https://docs.microsoft.com/en-us/office365/admin/security-and-compliance/set-up-multi-factor-authentication?redirectSourcePath=%252fen-us%252farticle%252fSet-up-multi-factor-authentication-for-Office-365-users-8f0454b2-f51a-4d9c-bcde-2c48e41621c6&view=o365-worldwide

If MFA (Multi Factor Authentication) is enabled on an account, you will not be able to connect to Office 365 using regular PowerShell. You would need to download the MFA module and then connect to Office 365 using that PowerShell Module.

To find out how to connect PowerShell to Office 365 if MFA is enabled feel free to read this article:

Connect to Exchange Online PowerShell using multi-factor authentication: https://docs.microsoft.com/en-us/powershell/exchange/exchange-online/connect-to-exchange-online-powershell/mfa-connect-to-exchange-online-powershell?view=exchange-ps

Hopefully by now you know how to connect PowerShell to Office 365, if MFA is enabled on the account. you would know by now that every time you must connect you would need to load the Microsoft Exchange Online PowerShell Module.

I work on scripts so the tool that prefer to use is PowerShell ISE. This helps me write code and test it in one application. Since the Microsoft Exchange Online PowerShell Module is on my system all I must do is load the module in PowerShell ISE to connect using this application.

The module is in the following path. C:\Users\Clint.Oliveira\AppData\Local\Apps\2.0\

To connect to the MFA PowerShell module for Exchange you can the run the following syntax:

Import-Module $((Get-ChildItem -Path $($env:LOCALAPPDATA+"\Apps\2.0\") -Filter Microsoft.Exchange.Management.ExoPowershellModule.dll -Recurse ).FullName|?{$_ -notmatch "_none_"}|select -First 1)
$Session=New-ExoPSSession
Import-PSSession $Session -Verbose -AllowClobber

Or

$CreateEXOPSSession = (Get-ChildItem -Path $env:userprofile -Filter CreateExoPSSession.ps1 -Recurse -ErrorAction SilentlyContinue -Force | Select -Last 1).DirectoryName

. "$CreateEXOPSSession\CreateExoPSSession.ps1"

And then run the following to connect:

Connect-EXOPSSession -UserPrincipalName Admin@domain.com

Alternatively, you can Change Directory to the path:

CD (Get-ChildItem -Path $env:userprofile -Filter CreateExoPSSession.ps1 -Recurse -ErrorAction SilentlyContinue -Force | Select -Last 1).DirectoryName

And then run .\CreateExoPSSession.ps1

I hope that  you like the 3 methods of connecting to an MFA enabled account to Exchange Online PowerShell (Office 365)

Leave a Reply

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