Today I was working on a simple PowerShell script to update a Date/Time column in SharePoint 2010 – I simply needed to iterate through each list item; updating the value of a column to be today’s date.
As I was doing that, I thought “it sure would be nice if I could preserve the ‘modified by’ person while updating each of these list items.”
Well not only is this possible, but it’s almost too easy!
Here is a screenshot of my list before touching it with PowerShell, two list items – one created by the System Account and one created by me:
What I’m going to do using PowerShell is modify the “Title” of the second list item – the one created by me. However, I’m going to run this code on the server with the farm account.
The key piece is storing the value of the “Modified By” column into a variable so I can then put the value back into the column when I’m done.
Here is the simple PowerShell code to do this:
$web = Get-SPWeb http://siteurl $list = $web.lists["List Name"] $item = $list.items.getitembyid(2) $modifiedBy = $item["Editor"] #Editor is the internal name of the Modified By column $item["Title"] = "Modified in PowerShell" $item["Editor"] = $modifiedBy $item.Update() $list.Update() $web.Dispose()
And finally, here’s a screenshot after doing the PowerShell modification:
Cool!
Excellent! I used this to modify Created By for a few posts. Works like a charm.
And, you did your $web.Dispose, always good to see.
Hey Bob,
Glad this helped you out. Great to hear positive feedback! And yeah, always dispose()!
Cheers!
Ryan
Outstanding. Years after it was written, still helping out.
Thanks!