Set SharePoint Online list experience to classic or modern using PnP PowerShell

Disclaimer (as always)

I just documented what I did real quick. There’s a lot of room for improvement especially around error handling and possibly logging. Why don’t you go ahead and put your suggestions in a comment?

I had to quickly change the list experience in a number of libraries after migration from SharePoint OnPrem to SharePoint Online.

Naturally you can set this in the list settings:

Dialogue for setting the list experience in the list settings on the site.
Go to ‘Library settings’ and then ‘Advanced Settings’

Since I needed to do it more then once, the decision was easy – time to script it! Here’s the functions I quickly cobbled together:

Set-SPOListExperienceWeb

This will set the default list experience for the web.

function Set-SPOListExperienceWeb {
    param (
        [Parameter(Mandatory=$True)][String]$SiteURL, 
        [Parameter(Mandatory=$True)][ValidateSet('Classic','Modern')][String]$ListExperience
    )

    # Connect to Site
    Connect-PnPOnline -Url $SiteUrl -UseWebLogin

    # Get WebName
    $WebName = (Get-PnPWeb).Title

    # Confirm current default experience
    $CurrentListExperience = (Get-PnPFeature -Identity '52E14B6F-B1BB-4969-B89B-C4FAA56745EF' -Scope Web).DefinitionId.Length
    switch ($ListExperience) {
        Classic { 
            # Check if feature is already enabled
            if ($CurrentListExperience -eq 0) {
                # Opt out of modern list experience
                Enable-PnPFeature -Identity '52E14B6F-B1BB-4969-B89B-C4FAA56745EF' -Scope Web
                Write-Host "Default List Experience for '$WebName' successfully set to $ListExperience." -ForegroundColor Yellow
            }
            else {
                Write-Host "Default List Experience for '$WebName' already set to $ListExperience - no change was made." -ForegroundColor Yellow
            }
        }
        # Defaults to Modern
        Default {
            # Check if feature is already disabled
            if ($CurrentListExperience -eq 1) {
                # Disable Opt-Out-Feature
                Disable-PnPFeature -Identity '52E14B6F-B1BB-4969-B89B-C4FAA56745EF' -Scope Web
                Write-Host "Default List Experience for '$WebName' sucessfully set to $ListExperience." -ForegroundColor Yellow
            }
            else {
                Write-Host "Default List Experience for '$WebName' already set to $ListExperience - no change was made" -ForegroundColor Yellow
            }
        }
    }
}

Set-SPOListExperienceSite

This will set the default list experience for the site. All lists on this site with the experience set to ‘auto’ will inherit this setting.

function Set-SPOListExperienceSite {
    param (
        [Parameter(Mandatory=$True)][String]$SiteURL, 
        [Parameter(Mandatory=$True)][ValidateSet('Classic','Modern')][String]$ListExperience
    )

    # Connect to Site
    Connect-PnPOnline -Url $SiteUrl -UseWebLogin

    # Confirm current default experience
    $CurrentListExperience = (Get-PnPFeature -Identity 'E3540C7D-6BEA-403C-A224-1A12EAFEE4C4' -Scope Site).DefinitionId.Length
    switch ($ListExperience) {
        Classic { 
            # Check if feature is already enabled
            if ($CurrentListExperience -eq 0) {
                # Opt out of modern list experience
                Enable-PnPFeature -Identity 'E3540C7D-6BEA-403C-A224-1A12EAFEE4C4' -Scope Site
                Write-Host "Default List Experience for $SiteURL successfully set to $ListExperience." -ForegroundColor Yellow
            }
            else {
                Write-Host "Default List Experience for $SiteURL already set to $ListExperience - no change was made." -ForegroundColor Yellow
            }
        }
        # Defaults to Modern
        Default {
            # Check if feature is already disabled
            if ($CurrentListExperience -eq 1) {
                # Disable Opt-Out-Feature
                Disable-PnPFeature -Identity 'E3540C7D-6BEA-403C-A224-1A12EAFEE4C4' -Scope Site
                Write-Host "Default List Experience for $SiteURL sucessfully set to $ListExperience." -ForegroundColor Yellow
            }
            else {
                Write-Host "Default List Experience for $SiteURL already set to $ListExperience - no change was made" -ForegroundColor Yellow
            }
        }
    }
}

Set-SPOListExperienceList

This will set the default list experience for the list.

function Set-SPOListExperienceList {
    param (
        [Parameter(Mandatory=$True)][String]$SiteURL, 
        [Parameter(Mandatory=$True)][String]$ListName, 
        [Parameter(Mandatory=$True)][ValidateSet('Classic','Modern','Auto')][String]$ListExperience
    )

    # Connect to Site
    Connect-PnPOnline -Url $SiteUrl -UseWebLogin

    # Get List
    $List = Get-PnPList -Identity $ListName -Includes ListExperienceOptions
    if (-not $List) {
        Write-Host "List $ListName not found." -ForegroundColor Red
    }
    else {
        switch ($ListExperience) {
            Classic {
                # Check if feature is already enabled
                if ($List.ListExperienceOptions -ne 'Classic') {
                    # Set list experience to classic
                    $list.ListExperienceOptions = 2
                    Write-Host "List Experience for '$ListName' successfully set to $ListExperience." -ForegroundColor Yellow
                }
                else {
                    Write-Host "List Experience for '$ListName' already set to $ListExperience - no change was made." -ForegroundColor Yellow
                }
            }
            Modern {
                # Check if feature is already enabled
                if ($List.ListExperienceOptions -ne 'NewExperience') {
                    # Set list experience to modern
                    $list.ListExperienceOptions = 1
                    Write-Host "List Experience for '$ListName' successfully set to $ListExperience." -ForegroundColor Yellow
                }
                else {
                    Write-Host "List Experience for '$ListName' already set to $ListExperience - no change was made." -ForegroundColor Yellow
                }
            }
            # Defaults to inheriting list experience settings from site
            Default {
                # Check if feature is already enabled
                if ($List.ListExperienceOptions -ne 'Auto') {
                    # Set list experience to auto
                    $list.ListExperienceOptions = 0
                    Write-Host "List Experience for $ListName successfully set to $ListExperience." -ForegroundColor Yellow
                }
                else {
                    Write-Host "List Experience for $ListName already set to $ListExperience - no change was made." -ForegroundColor Yellow
                }
            }
        }
        # Update list settings
        $list.Update()
        Invoke-PnPQuery 
    }
}

Examples

Set the site’s default list experience to ‘classic’.

Import-Module Set-SPOListExperience.psm1
$SiteUrl = 'https://**********.sharepoint.com/sites/testallthethings'
Set-SPOListExperienceSite -SiteURL $SiteUrl -ListExperience 'Classic'

Set the default document library experience to ‘auto’.

Import-Module Set-SPOListExperience.psm1
$SiteUrl = 'https://**********.sharepoint.com/sites/testallthethings'
Set-SPOListExperienceList -SiteURL $SiteUrl -ListName 'Documents' -ListExperience 'Classic'

We want it all!

Well then. Here’s the whole thing.
You can save it as a module (e.g. ‘Set-SPOListExperience.psm1’) or just throw the functions in your script if you’re a slob / in a hurry.

function Set-SPOListExperienceWeb {
    param (
        [Parameter(Mandatory=$True)][String]$SiteURL, 
        [Parameter(Mandatory=$True)][ValidateSet('Classic','Modern')][String]$ListExperience
    )

    # Connect to Site
    Connect-PnPOnline -Url $SiteUrl -UseWebLogin

    # Get WebName
    $WebName = (Get-PnPWeb).Title

    # Confirm current default experience
    $CurrentListExperience = (Get-PnPFeature -Identity '52E14B6F-B1BB-4969-B89B-C4FAA56745EF' -Scope Web).DefinitionId.Length
    switch ($ListExperience) {
        Classic { 
            # Check if feature is already enabled
            if ($CurrentListExperience -eq 0) {
                # Opt out of modern list experience
                Enable-PnPFeature -Identity '52E14B6F-B1BB-4969-B89B-C4FAA56745EF' -Scope Web
                Write-Host "Default List Experience for '$WebName' successfully set to $ListExperience." -ForegroundColor Yellow
            }
            else {
                Write-Host "Default List Experience for '$WebName' already set to $ListExperience - no change was made." -ForegroundColor Yellow
            }
        }
        # Defaults to Modern
        Default {
            # Check if feature is already disabled
            if ($CurrentListExperience -eq 1) {
                # Disable Opt-Out-Feature
                Disable-PnPFeature -Identity '52E14B6F-B1BB-4969-B89B-C4FAA56745EF' -Scope Web
                Write-Host "Default List Experience for '$WebName' sucessfully set to $ListExperience." -ForegroundColor Yellow
            }
            else {
                Write-Host "Default List Experience for '$WebName' already set to $ListExperience - no change was made" -ForegroundColor Yellow
            }
        }
    }
}

function Set-SPOListExperienceSite {
    param (
        [Parameter(Mandatory=$True)][String]$SiteURL, 
        [Parameter(Mandatory=$True)][ValidateSet('Classic','Modern')][String]$ListExperience
    )

    # Connect to Site
    Connect-PnPOnline -Url $SiteUrl -UseWebLogin

    # Confirm current default experience
    $CurrentListExperience = (Get-PnPFeature -Identity 'E3540C7D-6BEA-403C-A224-1A12EAFEE4C4' -Scope Site).DefinitionId.Length
    switch ($ListExperience) {
        Classic { 
            # Check if feature is already enabled
            if ($CurrentListExperience -eq 0) {
                # Opt out of modern list experience
                Enable-PnPFeature -Identity 'E3540C7D-6BEA-403C-A224-1A12EAFEE4C4' -Scope Site
                Write-Host "Default List Experience for $SiteURL successfully set to $ListExperience." -ForegroundColor Yellow
            }
            else {
                Write-Host "Default List Experience for $SiteURL already set to $ListExperience - no change was made." -ForegroundColor Yellow
            }
        }
        # Defaults to Modern
        Default {
            # Check if feature is already disabled
            if ($CurrentListExperience -eq 1) {
                # Disable Opt-Out-Feature
                Disable-PnPFeature -Identity 'E3540C7D-6BEA-403C-A224-1A12EAFEE4C4' -Scope Site
                Write-Host "Default List Experience for $SiteURL sucessfully set to $ListExperience." -ForegroundColor Yellow
            }
            else {
                Write-Host "Default List Experience for $SiteURL already set to $ListExperience - no change was made" -ForegroundColor Yellow
            }
        }
    }
}

function Set-SPOListExperienceList {
    param (
        [Parameter(Mandatory=$True)][String]$SiteURL, 
        [Parameter(Mandatory=$True)][String]$ListName, 
        [Parameter(Mandatory=$True)][ValidateSet('Classic','Modern','Auto')][String]$ListExperience
    )

    # Connect to Site
    Connect-PnPOnline -Url $SiteUrl -UseWebLogin

    # Get List
    $List = Get-PnPList -Identity $ListName -Includes ListExperienceOptions
    if (-not $List) {
        Write-Host "List $ListName not found." -ForegroundColor Red
    }
    else {
        switch ($ListExperience) {
            Classic {
                # Check if feature is already enabled
                if ($List.ListExperienceOptions -ne 'Classic') {
                    # Set list experience to classic
                    $list.ListExperienceOptions = 2
                    Write-Host "List Experience for '$ListName' successfully set to $ListExperience." -ForegroundColor Yellow
                }
                else {
                    Write-Host "List Experience for '$ListName' already set to $ListExperience - no change was made." -ForegroundColor Yellow
                }
            }
            Modern {
                # Check if feature is already enabled
                if ($List.ListExperienceOptions -ne 'NewExperience') {
                    # Set list experience to modern
                    $list.ListExperienceOptions = 1
                    Write-Host "List Experience for '$ListName' successfully set to $ListExperience." -ForegroundColor Yellow
                }
                else {
                    Write-Host "List Experience for '$ListName' already set to $ListExperience - no change was made." -ForegroundColor Yellow
                }
            }
            # Defaults to inheriting list experience settings from site
            Default {
                # Check if feature is already enabled
                if ($List.ListExperienceOptions -ne 'Auto') {
                    # Set list experience to auto
                    $list.ListExperienceOptions = 0
                    Write-Host "List Experience for $ListName successfully set to $ListExperience." -ForegroundColor Yellow
                }
                else {
                    Write-Host "List Experience for $ListName already set to $ListExperience - no change was made." -ForegroundColor Yellow
                }
            }
        }
        # Update list settings
        $list.Update()
        Invoke-PnPQuery 
    }
}

Export-ModuleMember 'Set-SPOListExperienceWeb'
Export-ModuleMember 'Set-SPOListExperienceSite'
Export-ModuleMember 'Set-SPOListExperienceList'

1 Comment on “Set SharePoint Online list experience to classic or modern using PnP PowerShell

Leave a Reply

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


The reCAPTCHA verification period has expired. Please reload the page.