Freigeben über


Using PowerShell scripts to modify document properties in SharePoint 2010

I recently had to find a way to add a value to the "Title" property of documents in a list.  There are many ways to do this, but PowerShell seems to be pretty powerful when it comes to this type of direct manipulation.  Since all my customer needed was to assign a value to the "Title" property, we decided to loop through all the documents and assign "Title" from "Name".  Here is a sample of that script.

  # Load SharePoint library
 [system.reflection.assembly]::LoadWithPartialName("Microsoft.Sharepoint")
 
 # Connect to the site collection https://SP2010 and store the object in the $site variable
 $site = New-Object Microsoft.SharePoint.SPSite("https://SP2010")
 
 # Connect to the root site in the site collection and store the object in $root
 $root = $site.rootweb
 
 # Store the Shared Documents document library in a variable $Docs
 $docs = $root.lists["Shared Documents"]
 
 # Display all the documents, their titles, names and IDs
 $docs.items | format-table -property title,name,id
 
 # Updates the title for each item in the list with Name
 $docs.items | ForEach { $_["Title"] = $_["Name"]; $_.Update() }
 
 # Display all the documents, their titles, names and IDs
 $docs.items | format-table -property title,name,id
 

The important line is where the items are being enumerated.  The tricky part is making sure you don't miss the period before the Update call.

Comments

  • Anonymous
    October 11, 2013
    The comment has been removed

  • Anonymous
    October 11, 2013
    The comment has been removed

  • Anonymous
    October 11, 2013
    Justin, Do you get values on the print-out prior to that?  It looks like you could have a null value in the name field. Paul

  • Anonymous
    November 20, 2013
    This is spot on exactly what I was trying to achieve on our site. Thanks a bunch Paul!

  • Anonymous
    November 20, 2013
    Thanks Gray - glad to see it helped you.  I haven't had to think about this in some time, so I'm glad to see what I wrote about helped.