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

# Connect to the site collection http://SP2010 and store the object in the $site variable
$site = New-Object Microsoft.SharePoint.SPSite("http://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 (5)
  1. Justin Gatewood says:

    Hey Paul,

    I've just tried your script, but I'm getting an error on the second to the last line where the $_["Name"]; portion is located.

    Any ideas what I can do to fix that issue? I'm at:



  2. Justin Gatewood says:

    To be specific, the response I'm getting in the shell is:

    //////////////////////////////ERROR FOLLOWS///////////////////////////////

    Cannot index into a null array.

    At line:1 char:42

    + $docs.items | ForEach { $_["Title"] = $_[ <<<< "Name"]; $_.Update() }

       + CategoryInfo                     : InvalidOperation :  (Name:String)  [ ], RuntimeException

       + FullyQualifiedErrorId       : NullArray

    You cannot call a method on a null-valued expression.

    At line:1 char:60

    + $docs.items | ForEach { $_["Title"] = $_["Name"]; $_.Update <<<< () }

       + CategoryInfo                     : InvalidOperation: (Update:String) [ ], RuntimeException

       + FullyQualifiedErrorId       : InvokeMethodOnNull

    //////////////////////////END OF ERROR/////////////////////////////////////////

    Any ideas?

    Thanks in advance for any assistance you can provide on this one.


  3. 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.


  4. Gray says:

    This is spot on exactly what I was trying to achieve on our site. Thanks a bunch Paul!

  5. 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.

Comments are closed.

Skip to main content