Tuesday, July 30, 2013

Update All Items in a SharePoint List using PowerShell

Use these PowerShell scripts to update all items in SharePoint lists:

SharePoint PowerShell script to update all items in list:
Requirement is to run a workflow which is configured to run on "On Change" event, on all items . So triggered update action on all items with this PowerShell script:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

### Variables ###
#Site URL and List names
$SiteURL = "http://intranet.crescent.com/"
$ListName = "Business Contacts"                 

#Get site and List objects
$web = Get-SPWeb $SiteURL
$List = $web.Lists.TryGetList($ListName)

if($List -ne $null)
    {    
        #Get all List items
        $ListItems = $list.Items
        
        #Iterate through each item
        Foreach($item in $ListItems)
        {
            #To update a particular field, $item[$FieldName]= value;

            #Perform an empty update
            $item.update()
        }
    }
#dispose web object
$web.Dispose() 

Filter and Update all documents in a SharePoint Library:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

### Variables ###
#Site URL and List names
$SiteURL = "http://intranet.crescent.com/Sites/Sales"
$ListName = "Sales Proposals" 
#Column to update
$FieldName ="Sales Category"
#Field old and new values
$OldValue = "Software"
$NewValue="Applications"

#Get site and List objects
$web = Get-SPWeb $SiteURL
$List = $web.Lists.TryGetList($ListName)

if($List -ne $null)
    {
        #Get all documents with .docx extension
        $DocsCollection = $List.Items | where {$_.Name -match ".docx"}
        
        #Iterate through each document
        foreach ($Doc in $DocsCollection)
        {
            #Change Category Metadata
            if($Doc[$FieldName] -eq $OldValue)
            {
                $Doc[$FieldName] = $NewValue

                #Perform a system update to keep versions, Modified by metadata intact
                $Doc.SystemUpdate($false)
                write-host "Updated an item at: $($doc.URL)"
            }
        }
    }

#dispose web object
$web.Dispose() 
This PowerShell script selectively updates all document's metadata, residing in a library - Including the one inside folders.

Here is an another trick to trigger update on multiple documents in SharePoint:
  • Navigate to your document library
  • Select required documents >> Click on "Check Out" button from ribbon
  • Click on "Check-in"
Update All Items in a SharePoint 2013



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...