0

Im trying to use the following ps script to update multiple items in a document list.

I think the example is related to sp 2010.

Anyone who have something to do this with?

$SPAssignment = Start-SPAssignment
$SPWeb = Get-SPWeb http://localserver/test -AssignmentCollection $spAssignment
$SPList = $SPWeb.Lists[“Shared Documents”]
$SPItems = $SPList.Items | Where { $_[“CustomField”] -eq “Yes” } 
foreach($item in $SPItems){
$item[“CustomField”] = “No”
$item.Update()
}
Stop-SPAssignment $SPAssignment
0

You can try several solutions, here two:

First one:

$SPWeb = Get-SPWeb http://localserver/test
$SPList = $SPWeb.Lists["Shared Documents"]
$SPItems = $SPList.Items | ? { ($_["HTML File Type"] -ne "SharePoint.DocumentSet") -and ($_["CustomField"] -eq "Yes") }
foreach($item in $SPItems){
    $item["CustomField"] = "No"
    $item.Update()
}

Source: Iterate through SharePoint library folders,subfolders and items

Second option:

Function GetMyFiles($Folder) {
    Write-Host " + in: "$Folder.Name
    foreach($file in $Folder.Files) {
        if($file["CustomField"] -eq "Yes"){
            Write-Host " `t" $file.Name
            $file["CustomField"] = "No"
            $file.Update()
        }
    }

    #Loop through all subfolders and call the function recursively
    foreach ($SubFolder in $Folder.SubFolders){
        if($SubFolder.Name -ne "Forms"){
            Write-Host "` t" - NoNewline
            GetMyFiles($Subfolder)
        }
    }

}

$SPWeb = Get-SPWeb http://localserver/test
$SPList = $SPWeb.Lists["Shared Documents"]
GetMyFiles($SPList.RootFolder)

Source: Iterating through all Document Libraries, Folder and Item using Powershell

  • Thank you Sergio! I think second option will work for me since i have subfolders i Shared Documents, but as i see it that example dont look for field, in this case named "CustomField" ? I need it to iterate through all files and sub folders in shared documents, WHERE "CustomFild" is set to "Yes", but only on files. Will that be possible? – Mark Petersen Feb 6 '18 at 11:14
  • Yes, you are right, an if was missing. I have already added it. It will do what you need. Please mark the answer as correct if it was helpful :) – Sergio Feb 6 '18 at 12:16
  • Hmm it works perfectly, or without error at least :) Its a start... However its hard to debug on :D ... I have several pdf files within Shared Documents and a subfolder with CustomField = Yes, but the PS script dont change the value to No ... – Mark Petersen Feb 7 '18 at 10:29
  • If Write-Host " t" $file.Name isn't printing the name of those pdf that means that is not entering in the IF for some reason. Has some file been changed to No? Check why – Sergio Feb 7 '18 at 23:04
  • Roger. The Write-Host " t" $file.Name returning t - NoNewline :-( I have tried different kind of field types i.e. Single line of Text but without luck.. – Mark Petersen Feb 8 '18 at 7:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.