public/Save-SkylineFieldSchema.ps1

Function Save-SkylineFieldSchema {
    <#
    .SYNOPSIS
    Extracts the PnP Provisioning Schema XML for the specific site fields(s)
     
    .DESCRIPTION
    Extracts the PnP Provisioning Schema XML for the specific site fields(s)
     
    Reference: https://github.com/SharePoint/PnP-Provisioning-Schema
 
    .EXAMPLE
    Save-SkylineFieldSchema -Field "SiteField1" -Path "C:\ProvisioningSchemas\SiteField1.xml"
 
    .PARAMETER Field
    Title of the target field. May pass multiple fields as -ContentType "SiteField1", "SiteField1"
 
    .PARAMETER Path
    Path to export the provisioning schema to. Example: C:\ProvisioningSchemas\SiteField1.xml
 
    .PARAMETER Web
    The web to apply the command to. Omit this parameter to use the current web.
    #>


    [cmdletbinding()]
    param(
        [parameter(Mandatory = $True)]
        [string[]]$Field, 
        [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("Field") | 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 Fields -Out $TemporaryPath -Force @PSBoundParameters

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

            $FieldNodesToRemove = @($SchemaXml.Provisioning.Templates.ProvisioningTemplate.SiteFields.Field | Where-Object {!$Field.Contains($_.DisplayName)})
            $FieldNodesToRemove | ForEach-Object {$SchemaXml.Provisioning.Templates.ProvisioningTemplate.SiteFields.RemoveChild($_) | Out-Null}

            #Save the remaining XML
            $SchemaXml.Save($Path)

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