Monday, January 30, 2012

Powershell Delete Unused WF Column

This script works as a function – meaning that there are two parts to running it in the SharePoint 2010 Management Shell. First, you have to run the script shown below:
function Delete-WorkflowColumn ($webURL, $listName, $columnName)
{
    #Setup variables from the user input
    $web = Get-SPWeb $webURL
    $list = $web.Lists[$listName]
    $column = $list.Fields[$columnName]
   
    #Make sure the column is not hidden or read only
    $column.Hidden = $false
    $column.ReadOnlyField = $false
    $column.Update()

    #Delete column and dispose of the web object
    $list.Fields.Delete($column)
    write-host "Deleted column"
    $web.Dispose()
}


Once the function has been run, you can call it to actually remove a column with the following command:
Delete-WorkflowColumn -webURL http://portal -listName "Documents" -columnName "Approval Workflow"

The example above deletes the “Approval Workflow” column from all list views of a document library called “Documents” in the site http://portal/.

Whilst I have focused in this article on removing columns generated by unused workflows, you can use exactly the same script to remove columns from any list or document library.