Skip to content

Update Author, Editor, Modified, Created

July 3, 2012

Here is a script to do a bulk update of file properties, including the pesky properties describing the read-only Author and Editor and the times in which the file was Created and Modified.

To do the bulk upload, first we create a tab delimited file with a column for the complete document URL, and one column for each property that we need to update.  A file like that will either be created from a secondary system, or be created by users.

There are three keys to the script.  First we use the import-csv command to read the tab delimited file.  Of course, the file can have other delimiters…  Once we read the file with that command, we can access each value as a child property of each row.  In order for this to work, the first row of the file should include the column names.

The second point is the way we find the file in SharePoint.  For this we use the built-in -match operator to apply a RegularExpression to break the file name on the last forward slash (/).  We open the Site and Web using the c# methods, because they take the entire URL  to open the right SPWeb.  Then, we use the split string to get the Folder and from it the File.

Lastly, we use the SystemUpdate function and pass False to make sure that no new version is created, and that the update statement is not setting the file date stamp.

Here is the entire script:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ea silentlycontinue
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

## get current location
$invocation = (Get-Variable MyInvocation -Scope 0).Value
$cmdPath = Split-Path $Invocation.MyCommand.Path
$p = "$cmdPath\fileProperties.txt" | import-csv -delimiter "`t"

<#
$_.CreatedBy
$_.CreatedDate
$_.DocumentUrl
$_.ExternalCode
$_.ExternalURL
$_.ModifiedBy
$_.ModidfiedDate
#>
$p | foreach {
try {

Write-Host $_.DocumentUrl

$site = new-object Microsoft.SharePoint.SPSite($_.DocumentUrl)
$web = $site.OpenWeb()
$_.DocumentUrl -match "(.*)/(.+)" > $null
$file = $web.GetFolder($matches[1]).Files[$matches[2]]

$item = $file.Item
$item["External Code"] = $_.ExternalCode
$item["External URL"] = $_.ExternalURL
$item["Created"] = $_.CreatedDate
$item["Modified"] = $_.ModifiedDate

#prepare for changes to read-only fields
$item.Fields["Created By"].ReadOnlyField = $false
$item.Fields["Modified By"].ReadOnlyField = $false
$item.SystemUpdate($false)

$user = $web.EnsureUser($_.CreatedBy)
$item["Created By"] = $user
$user = $web.EnsureUser($_.ModifiedBy)
$item["Modified By"] = $user
$item.SystemUpdate($false)

$item.Fields["Created By"].ReadOnlyField = $true
$item.Fields["Modified By"].ReadOnlyField = $true
$item.SystemUpdate($false)
}

catch [System.Exception] {
write-host -foregroundcolor red $_
}

finally {
if ($site) {
$site.dispose()
}

if ($web) {
$web.AllowUnsafeUpdates = $false
$web.Dispose()
}
}
}
Advertisements
Leave a Comment

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

%d bloggers like this: