SharePoint: Change owner on all files in all document libraries


$url = "http://sharepointurl/site"
#Change to the correct site

$SPWeb = Get-SPWeb $Url

$NewSPAuthor = New-Object Microsoft.SharePoint.SPFieldUserValue($SPWeb,52, "#Kneefel, L.A. (Lex)")
#Change to the correct userid value and displayname
#Unupported tip: Open SQL Management Studio, open the content database and open the table [dbo].[UserInfo]
#The column [tp_ID] contains the user id's
#The column [tp_Title] contains the displayname

#Note: You have to change the Editor to change the Author.
#To preserve the Original modifieddate and Editor the values are saved and later on written back

foreach($list in $spweb.Lists)
{
	if($list.BaseTemplate -eq "DocumentLibrary")
	{
		foreach ($item in $list.Items)
		{
			If ($list.Title -eq "Master Page Gallery"){continue}
			ElseIf ($list.Title -eq "Web Part Gallery"){continue}
			ElseIf ($list.Title -eq "Theme Gallery"){continue}
			ElseIf ($list.Title -eq "List Template Gallery"){continue}
			Else
			{
				$item.Url
				$OriginalEditor = $item["Editor"]
				$OriginalEditorLoginName = $item.properties["vti_modifiedby"]
				$OriginalModifiedDate = $item["Modified"]
				$item["Author"] = $NewSPAuthor
				$item.Properties["vti_Author"] = $NewSPAuthor.LoginName
				$item["Editor"] = $NewSPAuthor
				$item.properties["vti_modifiedby"] = $NewSPAuthor.LoginName
				$item["Modified"] = $OriginalModifiedDate
				$Item.UpdateOverwriteVersion()
				$item["Editor"] = $OriginalEditor
				$item.properties["vti_modifiedby"] = $OriginalEditorLoginName
				$item["Modified"] = $OriginalModifiedDate
				$Item.UpdateOverwriteVersion()
			}
		}
	}
}

Leave a Reply

Your email address will not be published.