Delete SharePoint empty site columns with Powershell Script

…this post was published on 2015 July 7.

# Add SharePoint Snapin to PowerShell
if((Get-PSSnapin | Where {$_.Name -eq “Microsoft.SharePoint.PowerShell”}) -eq $null) {
Add-PSSnapin Microsoft.SharePoint.PowerShell

$site = Get-SPSite(“http://shp2013-main/”)
$str = (“My_column”)
$lists = New-Object Collections.ArrayList
$i = 0

foreach($web in $site.AllWebs) {
foreach($list in $web.Lists) {
foreach ($field in $list.Fields) {
if($field.InternalName.Contains($str) -or ($field.Title -eq $str) ) {
Write-Host “‘$web’ web – Found a match in the ‘$field’ field in the ‘$list’ list”
Write-Host “Let’s check for column’s values”
foreach ($item in $list.Items){
$column_value=$column_value + $item[$str].Length
if($column_value -eq 0){
$lists.Insert($i, $list)
#will delete all existing empty columns founded before
if ($lists.Count -ne 0){
foreach($list in $lists){
“In this list ‘$list.title’ the field ‘$str’ is empty and will be deleted”
echo “Finished”

This entry was posted in Powershell, Sharepoint 2013. Bookmark the permalink.