Updating the “Created By” and “Modified By” Columns in SharePoint lists using POWERSHELL


Thinks

Ever had the good old System Account show up as your author or editor, oops I mean “Created By” and “Modified By” column in SharePoint lists? Come on, you know that you have. OK, if you have and wanted to modify these, it can be done through the good old SharePoint Object Model. Sowmyan has a post on doing this in C# from a quick little app and I wanted to take the time to play with my new favorite tool (PowerShell!)

Sample Code for your PowerShell file – hint user PowerGUI Script editor for good old code completion goodness (www.PowerGui.org). This is sample code that you should add the appropriate dispose and error checking etc to. The # is the PowerShell line comment code for those learning PowerShell.

The fun thing is that this could probably be done as a ONE LINE code thing as well.

 

# Get the SharePoint Assembly
# You will need to run this on the SharePoint Server

[Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")

# The site my list was in was at http://dev
# create a new object called $SPSite

$SPSite = New-Object Microsoft.SharePoint.SPSite("http://dev/")

# Make sure you have the last “/” in the url on the site
# allow $SPWeb to be the OpenWeb from the site

$SPWeb = $SPSite.OpenWeb()

# Get the list ModifyCreatedBy

$SPList = $SPWeb.Lists["ModifyCreatedBy"]

# Get the Collection of the List Items

$SPListItemCollection = $SPList.Items

# iterate the Collection

foreach ($ListItem in $SPListItemCollection)
    {

# The user in this case was Test R. Test and had a user id of 8 from the SPWeb users
#

        $SPFieldUserValue = New-Object Microsoft.SharePoint.SPFieldUserValue($SPWeb,8, "#Test R. Test")

        $ListItem["Author"] = $SPFieldUserValue                   

# Note: Editor will be the account that you are running the Powershell under unless you update # Editor as well

        $ListItem.Update()
    }

    $SPWeb.Update()

# Still TODO Disposes, Error Checking, Change to take Parameters, etc

 

Links

Clinks

  • GREAT! Thank you Sowmyan for sharing your post with the EcoSystem and providing me the motivation to do this in PowerShell
  • NOT SO GREAT! Unrelated, but my VPC had a disk error, argh.
  • GREAT! Was very straight forward in PowerShell!
Comments (10)

  1. Entwicklung What you need to know about AllowUnsafeUpdates (Part 1) What you need to know about AllowUnsafeUpdates

  2. Direkter Download: SPPD-099-2008-06-06 Aktuell SharePointCommunityCamp September 2008 (15.9./16.9.2008)

  3. Direkter Download: SPPD-099-2008-06-06 Aktuell SharePointCommunityCamp September 2008 (15.9./16.9.2008

  4. Direkter Download: SPPD-100-2008-06-25 Behind the Scene – QIK Video Aktuell SharePointCommunityCamp September

  5. Direkter Download: SPPD-100-2008-06-25 Behind the Scene – QIK Video Aktuell SharePointCommunityCamp September

  6. Direkter Download: SPPD-100-2008-06-25 Behind the Scene – QIK Video Aktuell SharePointCommunityCamp September

  7. Lordi says:

    Thank you very much, now I can get my self-updating inventory list done

  8. Clark says:

    You can also edit the created by value using a custom form and a Sharepoint:FormField pointed to Author.

    Only people with full control of the list will be able to edit though, for some reason.

  9. Westley says:

    Note that this can be done on document libraries too as long as you have rights. This powershell code assumes you have a valid list item in $itm, and a SPFieldUserValue in $oUser. Note you have to change the editor as well as author to get it to take-

    $itm["Author"].tostring() #show current author

    $itm["Author"] = $oUser #list item author

    $itm.Properties["vti_author"] = $oUser.User.LoginName #file properties author

    $itm["Editor"] = $oUser #this needs to be changed as well to update the item.

    $itm.UpdateOverwriteVersion()

    $itm["Author"].tostring() #print the changed author

  10. zima says:

    Can I do this in Workflow (sharepoint 2007) ?