Share via

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()
Microsoft 365 and Office | SharePoint Server | For business
Microsoft 365 and Office | SharePoint | Development
Microsoft 365 and Office | SharePoint Server | Development

1 answer

Sort by: Most helpful
  1. Yi Lu_MSFT 17,701 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.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.