public/Save-SkylineListSchema.ps1

Function Save-SkylineListSchema {
    <#
    .SYNOPSIS
    Extracts the PnP Provisioning Schema XML for the specific list(s)
     
    .DESCRIPTION
    Extracts the PnP Provisioning Schema XML for the specific list(s)
     
    Reference: https://github.com/SharePoint/PnP-Provisioning-Schema
 
    .EXAMPLE
    Save-SkylineListProvisioningSchema -List "Documents" -Path "C:\Schemas\Documents.xml"
 
    .PARAMETER List
    Title of the target list. May pass multiple lists as -List "List1", "List2"
 
    .PARAMETER Path
    Path to export the schema to. Example: C:\Schemas\Documents.xml
 
    .PARAMETER Web
    The web to apply the command to. Omit this parameter to use the current web.
     
    #>


    [cmdletbinding()]
    param(
        [parameter(Mandatory = $True)]
        [string[]]$List, 
        [parameter(Mandatory = $True)]
        [string]$Path,
        [Microsoft.SharePoint.Client.Web]$Web
    )

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

        Try
        {     
            $PSBoundParameters.Remove("List") | Out-Null
            $PSBoundParameters.Remove("Path") | Out-Null

            $Directory = Ensure-SkylineDirectory $Path
            $FileName = Get-SkylineFileName $Path
            $Path = "$Directory\$FileName"
            $TemporaryPath = "$Directory\$FileName.temp.xml"

            Get-PnPProvisioningTemplate -Handlers Lists -Out $TemporaryPath -Force @PSBoundParameters

            [XML]$SchemaXml = Get-Content -Path $TemporaryPath

            #Find XML nodes to remove which don't match the passed -List
            $ListNodesToRemove = @($SchemaXml.Provisioning.Templates.ProvisioningTemplate.Lists.ListInstance | Where-Object {!$List.Contains($_.Title)})

            #Remove the XML nodes from the main XML document
            $ListNodesToRemove | ForEach-Object {$SchemaXml.Provisioning.Templates.ProvisioningTemplate.Lists.RemoveChild($_) | Out-Null}

            #Save the remaining XML nodes of the passed -List to -Path
            $SchemaXml.Save($Path)

            Remove-Item -Path $TemporaryPath -Force
        }
        Catch
        {
            Throw $_
        }
    }
}