I have a powershell script that I am trying to use to add a column (Created) to a specific view in a specific list in multiple webs. The list exists in every sub web. The view exists in every list. There are hundreds of sub webs that I need to traverse through.
So far I have the following but it is not finding any lists that match the $listname even though the lists exist:
$site = Get-SPSite "http://sharepoint/sitecollection"
$column = "Created"
$listname = "My List Name"
$viewname = "My View Name"
$site | Get-SPWeb -limit all | ForEach-Object {
# Get All Lists Where Title Is ...
$lists = $_.Lists | where {
$_.Title -eq $listname
}
write-output (" Lists Count : " + $lists.Count)
# Loop libraries
for ($i = 0; $i -lt $lists.Count; $i++)
{
try
{
# Get current view
$view = $lists[$i].Views[$viewname]
if($view)
{
# Delete if already exist
while($view.ViewFields.ToStringCollection().Contains($column))
{
write-output (" Deleting Column From: " + $site.ToString())
$view.ViewFields.delete($column)
$view.Update()
}
# Add column
if(!$view.ViewFields.ToStringCollection().Contains($column))
{
write-output (" Adding Column To: " + $site.ToString())
$view.ViewFields.add($column)
$view.Update()
}
}
}
catch [Exception]
{
write-output (" Error: " + $_.Exception.ToString())
}
}
}
$site.Dispose()
How can I get all the lists with a specific name in multiple sites?