Found plenty of scripts but they are not with MFA. MFA is a standard now, do not understand why people insist of creating scripts not considering that.

Can someone help?

  • Do you need unattended script or attended? For attended you can still use interactive flag?
    – Rohit
    Commented May 18, 2023 at 9:59
  • No idea what those things mean, I need to run the script to get subsites to a site that should not contain more than 200 and either get the result on the screen or in a CSV
    – susan
    Commented May 18, 2023 at 10:20

2 Answers 2


Try using this PnP PowerShell script for getting all subsites in a SharePoint site collection:

$siteUrl = "https://crescent.sharepoint.com/sites/marketing"
Try {
    # Connect to SharePoint online site
    Connect-PnPOnline -Url $siteUrl -Interactive
    # Get all subsites in SharePoint Online site using PnP PowerShell
    $subsites = Get-PnPSubWeb -Recurse -IncludeRootWeb
    # Loop through each web in site collection
    ForEach($web in $subsites)
        Write-host $web.Url
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red

Check below threads for more information about authentication with MFA:

  1. Use App Password to Authenticate with PowerShell PnP
  2. The sign-in name or password does not match one in the Microsoft account system

Documentation: Get-PnPSubWeb

Connect-PnPOnline -Url "https://contoso.sharepoint.com" -UseWebLogin  


Connect-PnPOnline -Url "https://contoso.sharepoint.com" -Interactive  

Try either of these and then execute your commands. You should be prompted for credentials and MFA details.

Connecting to the site is first step. Once you have connection established. You can execute command below which gives all subsites for given site.

Get-PnPSubWeb  -Recurse -IncludeRootWeb

More details here : https://learn.microsoft.com/en-us/answers/questions/499551/unable-to-connect-to-pnp-powershell-for-sharepoint


  • I need the complete script of getting all the subsites to a site with MFA connection, I was able to find that myself
    – susan
    Commented May 18, 2023 at 11:03
  • 1
    Sure. added more details. Little humility will help though.
    – Rohit
    Commented May 18, 2023 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.