I am using below script to iterate through each document library and create a column with the value equals to the name of library itself:
$ver = $host | select version
if($Ver.version.major -gt 1) {$Host.Runspace.ThreadOptions = "ReuseThread"}
if(!(Get-PSSnapin Microsoft.SharePoint.PowerShell -ea 0))
{
Write-Progress -Activity "Loading Modules" -Status "Loading Microsoft.SharePoint.PowerShell"
Add-PSSnapin Microsoft.SharePoint.PowerShell
}
$SourceWebURL = "http://sitecollection/sites/sitename"
$ssite = Get-SPSite $SourceWebURL
$sweb = $ssite.OpenWeb("Op")
foreach($slist in $sweb.Lists)
{
if($slist.BaseType -eq "DocumentLibrary")
{
$spFieldType = [Microsoft.SharePoint.SPFieldType]::Text
$sList.Fields.Add("OpCo",$spFieldType,$true)
$field = $sList.Fields["OpCoName"]
$field.DefaultValue = $slist.Title
$field.Update()
$sList.Update()
}
}
I am getting below error:
An error occurred while enumerating through a collection: Collection was modifi
ed; enumeration operation may not execute..
At C:\Users\abc.ps1:31 char:8
+ foreach <<<< ($slist in $sweb.Lists)
+ CategoryInfo : InvalidOperation: (Microsoft.Share...on+SPEnumer
ator:SPEnumerator) [], RuntimeException
+ FullyQualifiedErrorId : BadEnumeration