> ## Content Index
> Fetch the complete content index at: https://blog.lsonline.fr/llms.txt
> Use this file to discover other available public pages before exploring further.

# PnP PowerShell and Multi-Factor Authentication
- URL: https://blog.lsonline.fr/2018/12/27/pnp-powershell-and-multi-factor-authentication/
- Published: 2018-12-27T17:17:51.000Z
- Updated: 2026-01-27T18:08:03.000Z
- Description: To increase the security of your environment, you should activate MFA on accounts that have some privileges on your Microsoft 365 Tenant... But, how to keep your script that runs self-sufficiently 🚀 (without user interaction)
- Author: Laurent Sittler
- Tags: PowerShell, Security, SharePoint Online, SharePoint, Microsoft 365

When you manage a Microsoft 365 Tenant, you often have to create accounts with some privileges/roles on the same Tenant. For example, an account as SharePoint Online administrator 🙄.

Once you have created a Microsoft 365 Tenant for your company, some rules of governance have to be respected and mainly those security ones. One of them, which is a Best Practice, consists of activating the MFA (Multi-Factor Authentication) on accounts that have a specific role(s) (especially if it is a service account).

You can configure the MFA to use text messages or mobile applications. I will show you how to configure it, expose you to the side effects of your PowerShell scripts, and how to deal with it.

> \[info\]**Important**  
>  
> In this use case, it is an account with the SharePoint administrator role.

## Why enable MFA?

The main reason is ***"increase security"***! Even if someone finds your password, he won't be able to connect to your account without validation or a code generated by your smartphone. And for other reasons like:

- Users cannot use App Password (I'll explain to you below what it is exactly) to connect the Office Portal
- App Password never expires (you can set "never expire password" for a specific account but it is not obvious to manage this kind of accounts and it is not recommended)
- Users can change their password and the App Password stays the same
- You can generate several App Passwords (I don't know if this case is really interesting but you can😁)
- You can revoke all App Password in one shot
- For security reasons, all accounts with admin role(s) should have MFA activated (especially for the Global Admin role)
- If an enterprise wishes to have a good score on Microsoft Secure Score, enabling MFA is recommended (CSA CCM301, Control DSI-02, FedRAMP Moderate, Control IA-3, GDPR, etc.)

There are still other differences/reasons, but here are the ones I expose to my customers and also why I generally advise activating the MFA.

## PowerShell script without MFA

In this example, we'll use the [PnP PowerShell](https://github.com/SharePoint/PnP-PowerShell?ref=blog.lsonline.fr) Framework to connect and count the number of SharePoint site collections of Tenant.

1. From the PowerShell console, use the connect cmdlet :

```PowerShell
Connect-PnPOnline -Url https://yourdomain-admin.sharepoint.com

```

1. A Windows prompt should appear. Fill out login ([your-user@yourdomain.onmicrosoft.com](mailto:your-user@yourdomain.onmicrosoft.com)) and password

![](https://blog.lsonline.fr/content/images/2018/12/LsOnline-WithoutMFA-PSScript-Connect-1.JPG)

1. Now, to show all SharePoint site collections, use the below cmdlet :

```PowerShell
(Get-PnPTenantSite).Count

```

![](https://blog.lsonline.fr/content/images/2018/12/LsOnline-WithoutMFA-PSScript-CountSC.JPG)

It works! 🚀🥳

## Configure MFA on an account

### Prerequisites

1. Connect to *Microsoft 365 Central Admin* with an account with sufficient rights
2. A smartphone
3. If you haven't already installed `Microsoft Authenticator App` on your device, you can do it from here:

[Microsoft Authenticator – Securely Access & Manage Your Online AccountsThe Microsoft Authenticator phone app gives you easy, secure access to online accounts, providing multi-factor authentication for an extra layer of security![](https://img-prod-cms-rt-microsoft-com.akamaized.net/cms/api/am/imageFileData/RE1Mu3b?ver=5c31)![](https://img-prod-cms-rt-microsoft-com.akamaized.net/cms/api/am/imageFileData/RWit7W?ver=ec28)](https://www.microsoft.com/en-us/account/authenticator?ref=blog.lsonline.fr)

### Configuration

1. From the *Microsoft Central Admin*, go to **Active users**
2. Click on the ellipsis located at the top bar and click on **Setup multifactor authentication**

![](https://blog.lsonline.fr/content/images/2018/12/LsOnline-MFA-ActiveUsers.JPG)

1. From the *Multi-factor authentication page*, select the target account that you want to enable MFA
2. To the right side, click on **Enable**

![](https://blog.lsonline.fr/content/images/2018/12/LsOnline-MFA-SelectUser.JPG)

1. From the dialog that will appear, click on **enable multi-factor auth**
2. From the successful message dialog, click on **close**

![](https://blog.lsonline.fr/content/images/2018/12/LsOnline-MFA-EnablePopup.JPG)

![](https://blog.lsonline.fr/content/images/2018/12/LsOnline-MFA-UpdateSuccessPopup.JPG)

1. Logout the current user or change browser
2. Log in with the target account that enabled MFA on it
3. Configure the Mobile App Authenticator
4. Generate your first App Password and save it somewhere

> \[note\]**Note**  
>  
> Here, the link to access directly to the App Password manager: [https://account.activedirectory.windowsazure.com/AppPasswords.aspx](https://account.activedirectory.windowsazure.com/AppPasswords.aspx?ref=blog.lsonline.fr)

That's done! 👏

## PowerShell script with MFA

1. From a new PowerShell console, use the connect cmdlet :

```PowerShell
Connect-PnPOnline -Url https://yourdomain-admin.sharepoint.com

```

1. A Windows prompt should appear. Fill out login ([your-user@yourdomain.onmicrosoft.com](mailto:your-user@yourdomain.onmicrosoft.com)) and password (like previously). But this time, after authentication, you should have an error message like this one :

![](https://blog.lsonline.fr/content/images/2018/12/LsOnline-WithMFA-SPScriptError.JPG)

1. Now, try again, but this time, use the App Password instead of user password

![](https://blog.lsonline.fr/content/images/2018/12/LsOnline-WithMFA-SPScriptAppPassword.JPG)

It works again! 🥳 pfiou

---

### It still does not work!

Microsoft provides several methods of authentication and it is possible to block **Legacy authentication**.

![](https://blog.lsonline.fr/content/images/2019/07/LsOnline-MFA-LegacyAuth-Error-1.jpeg)

Connect-PnPOnline does not support SharePoint Online credentials

You can enable the *Legacy Authentication* with the following cmdlet:

```PowerShell
Connect-SPOService -Url https://contoso-admin.sharepoint.com
Set-SPOTenant -LegacyAuthProtocolsEnabled $true
```

> \[note\]**Note**  
>  
> You will have to wait a couple of times before the changes take effect... [Microsoft announces 24 hours](https://docs.microsoft.com/en-ca/azure/active-directory/conditional-access/block-legacy-authentication?ref=blog.lsonline.fr#what-you-should-know), but in my cases, it never took more than 15 minutes.

> \[note\]Note  
>  
> In other use cases, MFA and App Password will not work, especially if you're using Microsoft Graph API. Other ways are available to connect to Microsoft 365 services like certificate or App Client/ID.
  
  
Hoping this post will help you 🚀