Changing the author field in SharePoint

An action nobody should do, but in some cases necessary: changing the author of an item.

What do you need?
  • a farm account
  • PoweShell
And this script:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Add-PSSnapin Microsoft.SharePoint.PowerShell -EA 0

#specify new user
$newuser = "i:0#.w|DOMAIN\username"

#specify the web 
$site = get-SPWeb "https://yoursite/"
$site.AllowUnsafeUpdates = $true

#specify the list name and item
$list = $site.Lists["Listname"]
$listitem = $list.GetItemById(1)

#get the user and prepare the userfield value
$user = get-SPuser -Web $site -Identity $newuser
$userString = "{0};#{1}" -f $user.ID, $user.UserLogin.Tostring()

#set new author
$listitem["Author"] = $userString
$listitem.UpdateOverwriteVersion()

write-host $listitem["Title"] "has been updated. The author has been set to $user"

$site.Update()
$site.Dispose()

Be aware!
This script should be used wisely.

Comments

Popular posts from this blog

How to support multiple languages in WPF