powershell for updating columns values in document library

gogi100 51 Reputation points
2022-04-01T19:57:36.783+00:00

i have a the powershell script where i want that update columns of documents in folders based on column values of folders in whom those files exists. my code is but he is wrong, where is error

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

 ####
 #### Update sharepoint field
 ###

#Get the List, update fields
$spSite = Get-SPSite -Identity http://test-net.dri.local
$website = $spSite.OpenWeb()
$list = $website.Lists["Лична акта"]
$items = $list.Items

foreach($folder in $list.Folders)
{
$imeiprezime=$folder["Име и презиме"]
$korisnickoime=$folder["Корисничко име"]
$eposta=$folder["Е-пошта"]
foreach($item in $folder.LisItems)
{
    if($item["Име и презиме"] -eq "")
    {
        $item["Име и презиме"] = $imeiprezime
        $item.Update()
    }
    if($item["Корисничко име"] -eq "")
    {
        $item["Корисничко име"] = $korisnickoime
        $item.Update()
    }
    if($item["Е-пошта"] -eq "")
    {
        $item["Е-пошта"] = $eposta
        $item.Update()
    }

}
}
$website.Dispose()
SharePoint Server
SharePoint Server
A family of Microsoft on-premises document management and storage systems.
2,227 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,675 questions
SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,575 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Yi Lu_MSFT 17,456 Reputation points
    2022-04-15T12:13:36.993+00:00

    Hi @gogi100
    You could try below code:

    Add-PSSnapin Microsoft.SharePoint.PowerShell -EA SilentlyContinue  
     $Web = Get-SPWeb "http://test-net.dri.local"  
      
     $list = $web.Lists["luyidoc"]  
     $items = $list.Items  
          
     foreach($folder in $list.Folders)  
     {  
     $imeiprezime=$folder["luyi"]  
     $eposta=$folder["luyi2"]  
        $query = New-Object -Type 'Microsoft.SharePoint.SPQuery'  
        $query.Folder = $folder.Folder  
        $folderItems = $list.GetItems($query)  
        foreach($item in $folderItems)  
     {  
          if($item["luyi"] -eq $null)  
    	{   
             $item["luyi"] = $imeiprezime  
             $item.Update()  
         }  
      if($item["luyi2"] -eq $null)  
    	{   
             $item["luyi2"] = $eposta  
             $item.Update()  
         }  
      }   
    }  
    

    Here you need to change the parameter as your own need.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.