Replace all instances of a user in Last Modified and Created By using PowerShell


If you for unknown reasons end up with the wrong user in the last modified and author field of documents and folder, or if you simply want to replace a user with a differerent user account, then you can use this script to do it in all documents and flders within an SPWeb.

The script below will iterate thru the Shared Documents library, replace every ‘Last Modified'(Editor) and ‘Created By'(Author) with the user with ID 405.

[Reflection.Assembly]::Load(“Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”)
# don’t leave out the last “/” in the url
$SPSite = New-Object Microsoft.SharePoint.SPSite(“http://www.mycompany.com/“)
$SPWeb = $SPSite.OpenWeb(“MySite”)
$SPList = $SPWeb.Lists[“Shared documents”]
$SPListItemCollection = $SPList.Items
foreach ($ListItem in $SPListItemCollection)  {
   #This example gets the user with ‘SharePoint ID’ of 405.
    $SPFieldUserValue = New-Object Microsoft.SharePoint.SPFieldUserValue ($SPWeb,405)
    If ($ListItem[“Editor”] -eq “1;#Chuck Norris (Admin)” -or $ListItem[“Editor”] -eq “385;#Bruce Lee”) {
        $ListItem[“Author”] = $SPFieldUserValue
        $ListItem[“Editor”] = $SPFieldUserValue
        $ListItem.Update()
    }
}

# Do the same for all folders

$SPListItemCollection = $SPList.folders
foreach ($ListFolder in $SPListItemCollection) {
    $SPFieldUserValue = New-Object Microsoft.SharePoint.SPFieldUserValue ($SPWeb,405)
    If ($ListFolder[“Editor”] -eq “1;#Chuck Norris (Admin)” -or $ListFolder[“Editor”] -eq “385;#Bruce Lee”) {
        $ListFolder[“Author”] = $SPFieldUserValue
        $ListFolder[“Editor”] = $SPFieldUserValue
       $ListFolder.Update()
    }
}
$SPWeb.Update()
$SPSite.Dispose()

Remember though, the last modified date will be updated to the current date.

Stay tuned!

Regards

Advertisement

2 thoughts on “Replace all instances of a user in Last Modified and Created By using PowerShell

  1. How can I do this to change it per folder? Let’s say I have a document library which parses out locations of documents and I need to change the documents modified by per folder??

    1. Hi Katie.
      I think that you can get the parent folder from each $listitem using something like:
      $istItemParentFolder = $ListItem.File.ParentFolder.Name
      then do something based on that value…?

      Will that work?
      Regards // Thomas

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s