public/Set-SkylineExperience.ps1

Function Set-SkylineExperience {
    <#
    .SYNOPSIS
    Sets the experience for a site collection or web to classic or modern
     
    .DESCRIPTION
    Sets the experience for a site collection or web to classic or modern
 
    .EXAMPLE
    Set-SkylineExperience -Mode Classic -Scope Site
 
    .EXAMPLE
    Set-SkylineExperience -Mode Modern -Scope Web
 
    .PARAMETER Mode
    Specify Classic or Modern experience
 
    .PARAMETER Scope
    Specify whether to apply the experience to the Web or Site
 
    .PARAMETER Web
    The web to apply the command to. Omit this parameter to use the current web.
     
    #>


    [cmdletbinding()]
    param(
        [parameter(Mandatory = $True)]
        [ValidateSet("Classic","Modern")]
        [string]$Mode,
        [parameter(Mandatory = $True)]
        [ValidateSet("Web","Site")]
        [string]$Scope,
        [Microsoft.SharePoint.Client.Web]$Web
    )

    Process
    {
        Write-Debug ( "Running $($MyInvocation.MyCommand).`n" + "PSBoundParameters:`n$($PSBoundParameters | Format-List | Out-String)")

        Try
        {     
            $PSBoundParameters.Remove("Mode") | Out-Null            
            $PSBoundParameters.Remove("Scope") | Out-Null  

            $WebFeatureId = "52E14B6F-B1BB-4969-B89B-C4FAA56745EF"
            $SiteFeatureId = "E3540C7D-6BEA-403C-A224-1A12EAFEE4C4"

            if ($Scope -eq "Web" -and $Mode -eq "Classic") 
            {
                try {
                    $Web = Get-PnPWeb @PSBoundParameters
                    $Web.Features.Add($WebFeatureId, $true, [Microsoft.SharePoint.Client.FeatureDefinitionScope]::None) | Out-Null
                    $Web.Context.ExecuteQuery()
                }
                catch { }
            }
            elseif ($Scope -eq "Web" -and $Mode -eq "Modern")
            {
                try {                    
                    $Web = Get-PnPWeb @PSBoundParameters
                    $Web.Features.Remove($WebFeatureId, $true)
                    $Web.Context.ExecuteQuery()
                }
                catch { }
            }
            elseif ($Scope -eq "Site" -and $Mode -eq "Classic")
            {
                try {
                    $Site = Get-PnPSite @PSBoundParameters
                    $Site.Features.Add($SiteFeatureId, $true, [Microsoft.SharePoint.Client.FeatureDefinitionScope]::None) | Out-Null
                    $Site.Context.ExecuteQuery()
                }
                catch { }
            }
            elseif ($Scope -eq "Site" -and $Mode -eq "Modern")
            {
                try {
                    $Site = Get-PnPSite @PSBoundParameters                
                    $Site.Features.Remove($SiteFeatureId, $true)
                    $Site.Context.ExecuteQuery()
                }
                catch { }
            }
        }
        Catch
        {
            Throw $_
        }
    }
}