如何使用 PowerShell 从 SharePoint 列表中删除列?
问题:用户创建的计算列的公式有错误!这导致列表视图 Web 部件崩溃!无法使用 Web UI 从 SharePoint 列表中删除该列。
如何从 SharePoint 列表中删除列?
您需要删除 SharePoint 列表中的列吗? PowerShell 让一切变得简单。在本文中,我们将引导您完成从 SharePoint 列表中删除列的过程。我们还将向您展示如何使用 PowerShell 从 SharePoint 列表中删除列。
在 SharePoint Web UI 中,要从列表或库中删除字段,请执行以下步骤:
用于从 SharePoint 列表中删除列的 PowerShell 脚本
如果您需要从 SharePoint 列表中删除列,但 Web 浏览器 UI 没有帮助,那么 PowerShell 就是您的最佳选择!以下是从列表中删除列的 PowerShell:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Variables
$SiteURL="https://intranet.crescent.com/"
$ListName="Change Request"
$ColumnName="Activity Status"
#Get Internal Name of the columns
$web = Get-SPWeb $SiteURL
#Get the list
$list = $web.Lists.TryGetList($ListName)
if($List -ne $null)
{
#Get the column
$column = $list.Fields[$ColumnName]
if($column -ne $null)
{
#Reset column properties to allow delete
$column.Hidden = $false
$column.ReadOnlyField = $false
$column.AllowDeletion = $true
$column.Update()
#delete column in sharepoint list
$list.Fields.Delete($column)
write-host "Column has been deleted!" -f Green
}
else
{
write-host "Specified column name not found!" -ForegroundColor Red
}
}
使用 PowerShell 从 SharePoint 列表中批量删除列:
让我们将代码包装在一个函数中,并调用从 SharePoint 列表中批量删除列以使其可重用。
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Custom Function to delete a column with PowerShell
Function Remove-Column($WebURL, $ListName, $ColumnName)
{
#Get Internal Name of the columns
$web = Get-SPWeb $WebURL
#Get the list
$list = $web.Lists.TryGetList($ListName)
if($List -ne $null)
{
#Get the column
$column = $list.Fields[$ColumnName]
if($column -ne $null)
{
#Reset column properties to allow delete
$column.Hidden = $false
$column.ReadOnlyField = $false
$column.AllowDeletion = $true
$column.Update()
#Delete the column from list
$list.Fields.Delete($column)
write-host "Column '$ColumnName' has been deleted!" -f Green
}
else
{
write-host "Specified column name not found!" -ForegroundColor Red
}
}
else
{
write-host "Specified List is not found!" -ForegroundColor Red
}
}
#Variables
$WebURL="https://intranet.crescent.com"
$ListName="Change Request"
#Array to hold column titles
$ColumnNames=@("Change Type","Approvers","Proposed Date and Time")
#Process each column
$ColumnNames | foreach {
#Call the function to delete column from sharepoint list
Remove-Column $WebURL $ListName $_
}
在某些情况下,您可能必须设置:“$column.Sealed=$false”,而不是“$Column.Hidden=$False”
与往常一样,请确保在删除任何列之前已备份数据。因为,当您删除某一列时,该列中的所有数据也会被删除。 (并且没有撤消或从回收站恢复选项!)
