使用 SharePoint Online PowerShell 从模板创建文档库

Create a Document Library from a Template using SharePoint Online PowerShell

如何使用 SharePoint Online PowerShell 从保存为模板的现有文档库创建新的文档库?

我正在尝试以下操作:

New-PnPList -Title "Test" -Template DocLibTemplate.stp -Url Test

我在 CSOM PowerShell 上运气不佳...PnP 似乎是可用的最佳选择。

以下PowerShell供您参考。

$ListTemplateInternalName="DocLibTemplate.stp"
$ListName ="PnpDocument"
Connect-PnPOnline -Url https://xx.sharepoint.com/sites/lz -Credential (Get-credential)

$Context = Get-PnPContext
$Web = $Context.Site.RootWeb
$ListTemplates = $Context.Site.GetCustomListTemplates($Web)
$Context.Load($Web)
$Context.Load($ListTemplates)
Invoke-PnPQuery

$ListTemplate = $ListTemplates | where { $_.InternalName -eq $ListTemplateInternalName }

if ($ListTemplate -eq $null)
{
    Throw [System.Exception] "Template not found"
}

$ListCreation = New-Object Microsoft.SharePoint.Client.ListCreationInformation
$ListCreation.Title = $ListName
$ListCreation.ListTemplate = $ListTemplate

$Web.Lists.Add($ListCreation)
Invoke-PnPQuery

Disconnect-PnPOnline