locked
Change owner of multiple documents RRS feed

  • Question

  • Hi Guys,

    I wonder if there is a method to change the Document owner for multiple documents in SharePoint 2013/2016?
    The documents are located in different libraries. I even wonder if there is any way to exclude some of the documents?

    Any tips should be very appreciated
    Regards
    Ashraf

    Monday, March 30, 2020 9:42 AM

Answers

  • Hi Ashraf,

    I just tested the script on my end and it worked just fine, Make sure you change the highlighted ones and then run the script. 

    If you want to change the Created By field then use Internal name of it called Author, Modified By filed Internal Name is Editor.

    In place of Editor place your field name from the below script.

    Add-PSSnapin Microsoft.SharePoint.Powershell
    $web = Get-SPWeb "https://portal.contoso.com/sites/test/"
    $library = $web.Lists["Documents"]
    $oldUser = $web.EnsureUser("Domain\OldUser")
    $newUser = $web.EnsureUser("Domain\NewUser")
    
    foreach ($item in $library.Items)
    {
       $userfield = New-Object Microsoft.SharePoint.SPFieldUserValue($web,$item["Editor"].ToString())
       $login = $userfield.User.LoginName
       if ($login -eq $oldUser.LoginName)
       {
          $item["Editor"] = $newUser
          #if you are using default "Author" column, you need to set the following as well:
          #$item.Properties["vti_author"] = $newUser.LoginName
          $item.UpdateOverwriteVersion() #this saves changes without incrementing the version
       }
       $login = $null
    }
    
    $web.Dispose();

    Thanks & Regards,


    Sharath Aluri

    Tuesday, March 31, 2020 5:42 PM
  • Hi Ashraf,

    Here’s another sample script for your reference.

    Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
      
    #Set Configuration Parameters
    $WebURL = "<SiteURL>"
    $ListName = "<Library>"
    $UserName = "<New Username>"
      
    #Get the Web, List, Item and User objects
    $Web = Get-SPWeb $WebURL
    $List = $web.Lists[$ListName]
    
    foreach ($Item in $List.Items) {
    	$Author = $web.EnsureUser($UserName)
    	  
    	#update the created by & modified by columns
    	$Item["Author"] = $Author
    	$Item["Editor"] = $Author
    	$Item.SystemUpdate()
    }

    I suggest you move all the documents created by this user to one site collection (or one library if possible) and then run the script to change the owner.

    You can refer to the link below for the sample script for moving documents.

    how to move multiple documents from one to another list in SharePoint 2013?

    Disclaimer: Microsoft provides third-party contact information to help you find technical support. This contact information may change without notice. Microsoft does not guarantee the accuracy of this third-party contact information.

    Best regards,

    Chelsea Wu


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Wednesday, April 1, 2020 8:29 AM

All replies

  • Try with the below sample powershell script.

    Add-PSSnapin Microsoft.SharePoint.Powershell
    $web = Get-SPWeb "http://server/sites/website"
    $library = $web.Lists["Display Name of your library"]
    $oldUser = $web.EnsureUser("DOMAIN\OldUserAccount")
    $newUser = $web.EnsureUser("DOMAIN\NewUserAccount")
    
    foreach ($item in $library.Items)
    {
       $userfield = New-Object Microsoft.SharePoint.SPFieldUserValue($web,$item["DocumentAuthorColumnInternalName"].ToString())
       $login = $userfield.User.LoginName
       if ($login -eq $oldUser.LoginName)
       {
          $item["DocumentAuthorColumnInternalName"] = $newUser
          #if you are using default "Author" column, you need to set the following as well:
          #$item.Properties["vti_author"] = $newUser.LoginName
          $item.UpdateOverwriteVersion() #this saves changes without incrementing the version
       }
       $login = $null
    }
    
    $web.Dispose();

    Below articles for your reference:

    https://www.collaboris.com/how-to-update-multiple-documents-in-a-sharepoint-document-library-with-powershell-2/

    https://sharepoint.stackexchange.com/questions/163792/sharepoint-2010-bulk-change-document-owner

    Thanks & Regards,


    sharath aluri

    Tuesday, March 31, 2020 3:28 AM
  • Hi Ashraf,

    Do you mean you want to update the “Created By” column value, or the permission for the documents?

    If you want to update the “Created By” column value for the documents, you can refer to the link below for a sample script to modify document by IDs.

    Update "Created By", "Last Modified" Metadata Fields of a List Item using PowerShell.

    If you want to update the permissions for the documents, here is another reference with sample script.

    Break Inheritance and Add-Remove Item Level Permission with PowerShell.

    Please feel free to post back if you need further assistance.

    In addition, we need to get the documents either by ID or by title or filename. It is recommended to put all the documents you want to update in one location.

    Disclaimer: Microsoft provides third-party contact information to help you find technical support. This contact information may change without notice. Microsoft does not guarantee the accuracy of this third-party contact information.

    Best regards,

    Chelsea Wu


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Tuesday, March 31, 2020 7:08 AM
  • Hi Chelsea,

    Thank you for your answer. I'll clarify what I ment with my post.

    Actually, my customer has an employee which she will leave the company soon. She owns the most of a couple of thousands of documents under HR library. Now the customer wish to change the ownership to a new employee.

    I tried to ran a script as below:

    Add-PSSnapin Microsoft.SharePoint.Powershell
    $web = Get-SPWeb "https://the customer's url/"
    $library = $web.Lists["The library"]
    $oldUser = $web.EnsureUser("domain\oldowner")
    $newUser = $web.EnsureUser("domain\newowner")

    foreach ($item in $library.Items)
    {
       $userfield = New-Object Microsoft.SharePoint.SPFieldUserValue($web,$item["DocumentAuthorColumnInternalName"].ToString())
       $login = $userfield.User.LoginName
       if ($login -eq $oldUser.LoginName)
       {
          $item["DocumentAuthorColumnInternalName"] = $newUser
          #if you are using default "Author" column, you need to set the following as well:
          #$item.Properties["vti_author"] = $newUser.LoginName
          $item.UpdateOverwriteVersion() #this saves changes without incrementing the version
       }
       $login = $null
    }

    $web.Dispose();

    But the get the error messages below:

     

    new-object : A constructor was not found. Cannot find an appropriate constructor for type Microsoft.SharePoint.SPSite.
    At C:\Scripts\ChagneOwner.ps1:7 char:9
    + $site = new-object Microsoft.SharePoint.SPSite($siteUrl)
    +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (:) [New-Object], PSArgumentException
        + FullyQualifiedErrorId : CannotFindAppropriateCtor,Microsoft.PowerShell.Commands.NewObjectCommand

    You cannot call a method on a null-valued expression.
    At C:\Scripts\ChagneOwner.ps1:8 char:1

    What's wrong with the script above?

    Regards

    Ashraf

     

    Tuesday, March 31, 2020 2:57 PM
  • Hi Sharath,

    I tried the script below:

    Add-PSSnapin Microsoft.SharePoint.Powershell
    $web = Get-SPWeb "https://the customer's url/"
    $library = $web.Lists["The library"]
    $oldUser = $web.EnsureUser("domain\oldowner")
    $newUser = $web.EnsureUser("domain\newowner")

    foreach ($item in $library.Items)
    {
       $userfield = New-Object Microsoft.SharePoint.SPFieldUserValue($web,$item["DocumentAuthorColumnInternalName"].ToString())
       $login = $userfield.User.LoginName
       if ($login -eq $oldUser.LoginName)
       {
          $item["DocumentAuthorColumnInternalName"] = $newUser
          #if you are using default "Author" column, you need to set the following as well:
          #$item.Properties["vti_author"] = $newUser.LoginName
          $item.UpdateOverwriteVersion() #this saves changes without incrementing the version
       }
       $login = $null
    }

    $web.Dispose();

    But I got the error messages below:

     

    new-object : A constructor was not found. Cannot find an appropriate constructor for type Microsoft.SharePoint.SPSite.
    At C:\Scripts\ChagneOwner.ps1:7 char:9
    + $site = new-object Microsoft.SharePoint.SPSite($siteUrl)
    +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (:) [New-Object], PSArgumentException
        + FullyQualifiedErrorId : CannotFindAppropriateCtor,Microsoft.PowerShell.Commands.NewObjectCommand

    You cannot call a method on a null-valued expression.
    At C:\Scripts\ChagneOwner.ps1:8 char:1

    What's wrong with the script above?

    Regards

    Ashraf

     

    Tuesday, March 31, 2020 2:59 PM
  • Hi Ashraf,

    I just tested the script on my end and it worked just fine, Make sure you change the highlighted ones and then run the script. 

    If you want to change the Created By field then use Internal name of it called Author, Modified By filed Internal Name is Editor.

    In place of Editor place your field name from the below script.

    Add-PSSnapin Microsoft.SharePoint.Powershell
    $web = Get-SPWeb "https://portal.contoso.com/sites/test/"
    $library = $web.Lists["Documents"]
    $oldUser = $web.EnsureUser("Domain\OldUser")
    $newUser = $web.EnsureUser("Domain\NewUser")
    
    foreach ($item in $library.Items)
    {
       $userfield = New-Object Microsoft.SharePoint.SPFieldUserValue($web,$item["Editor"].ToString())
       $login = $userfield.User.LoginName
       if ($login -eq $oldUser.LoginName)
       {
          $item["Editor"] = $newUser
          #if you are using default "Author" column, you need to set the following as well:
          #$item.Properties["vti_author"] = $newUser.LoginName
          $item.UpdateOverwriteVersion() #this saves changes without incrementing the version
       }
       $login = $null
    }
    
    $web.Dispose();

    Thanks & Regards,


    Sharath Aluri

    Tuesday, March 31, 2020 5:42 PM
  • Hi Ashraf,

    Here’s another sample script for your reference.

    Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
      
    #Set Configuration Parameters
    $WebURL = "<SiteURL>"
    $ListName = "<Library>"
    $UserName = "<New Username>"
      
    #Get the Web, List, Item and User objects
    $Web = Get-SPWeb $WebURL
    $List = $web.Lists[$ListName]
    
    foreach ($Item in $List.Items) {
    	$Author = $web.EnsureUser($UserName)
    	  
    	#update the created by & modified by columns
    	$Item["Author"] = $Author
    	$Item["Editor"] = $Author
    	$Item.SystemUpdate()
    }

    I suggest you move all the documents created by this user to one site collection (or one library if possible) and then run the script to change the owner.

    You can refer to the link below for the sample script for moving documents.

    how to move multiple documents from one to another list in SharePoint 2013?

    Disclaimer: Microsoft provides third-party contact information to help you find technical support. This contact information may change without notice. Microsoft does not guarantee the accuracy of this third-party contact information.

    Best regards,

    Chelsea Wu


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Wednesday, April 1, 2020 8:29 AM
  • Thanks a lot :)

    It works now

    Regards
    Ashraf

    Wednesday, April 1, 2020 10:03 AM
  • Many thanks :)

    It works now with the first script you have sent :)

    Regards
    Ashraf

    Wednesday, April 1, 2020 10:05 AM