Home > Powershell, SharePoint > Replace all instances of a user in Last Modified and Created By using PowerShell

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

About these ads
  1. Kathie
    July 21, 2012 at 23:05

    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??

    • July 22, 2012 at 05:38

      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

  1. No trackbacks yet.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 231 other followers

%d bloggers like this: