SharePoint Online - Change Document Title Using Powershell

Tom Molskow 386 Reputation points
2021-08-26T15:41:45.047+00:00

Hello Community,

I'm working in SharePoint Online and I want to change a document's title using PowerShell. Has anyone else done this? If so please provide guidance and code examples.

Thanks!

Tom

Microsoft 365 and Office SharePoint For business Windows
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Limitless Technology 39,916 Reputation points
    2021-08-27T08:42:49.097+00:00

    Hi,

    Can you try this out and repost if you have any issues

    Load SharePoint CSOM Assemblies

    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

    Variables

    $SiteURL="**************************************"
    $NewSiteTitle="United States"

    Try {
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials
    
    #Get the Site from URL
    $Web = $Ctx.web
    $Ctx.Load($web)
    $Ctx.ExecuteQuery()
    
    #Get the current site title
    Write-host "Site Title:"$web.title
    
    #sharepoint online powershell change site title
    $web.title = $NewSiteTitle
    $Web.Update()
    $Ctx.ExecuteQuery()
    
    Write-host "Site Title has been updated to '$NewSiteTitle'" -ForegroundColor Green 
    

    }
    Catch {
    write-host -f Red "Error Updating Site Title!" $_.Exception.Message
    }

    If an Answer is helpful, please click "Accept Answer" and upvote it : )


1 additional answer

Sort by: Most helpful
  1. Allen Xu_MSFT 13,861 Reputation points
    2021-08-27T02:36:45.333+00:00

    Hi @Tom Molskow ,

    Do you mean that you are going to rename a document using PowerShell, right? If yes, please take a reference to below PowerShell scripts.

    PowerShell CSOM Script to rename a document in SharePoint Online:

    #Load SharePoint CSOM Assemblies  
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"  
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  
         
    #Set Variables for Site URL, Old File Name and New File Name  
    $SiteURL= "https://contoso.sharepoint.com/sites/sales/"  
    $OldFileURL="/sites/Sales/Documents/Legal.docx"  
    $NewFileURL="/sites/Sales/Documents/LegalTemplate.docx"  
       
    #Setup Credentials to connect  
    $Cred = Get-Credential  
    $Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)  
       
    Try {  
        #Setup the context  
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)  
        $Ctx.Credentials = $Cred  
       
        #Rename the File  
        $File = $Ctx.Web.GetFileByServerRelativeUrl($OldFileURL)  
        $File.MoveTo($NewFileURL, [Microsoft.SharePoint.Client.MoveOperations]::Overwrite)  
        $Ctx.ExecuteQuery()  
       
        write-host -f Green "File Renamed successfully!"  
    }  
    Catch {  
        write-host -f Red "Error Renaming File!" $_.Exception.Message  
    }  
    

    Rename a document using PnP PowerShell:

    #Config Variables  
    $SiteURL = "https://contoso.sharepoint.com/sites/marketing"  
    $FileURL= "Shared Documents/Active/Office 365 Proposal.pdf"  
    $NewFileName ="Office 365 Proposal V2.pdf"  
       
    #Connect to PnP Online  
    Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)  
       
    #Rename the File  
    Rename-PnPFile -SiteRelativeUrl $FileURL -TargetFileName $NewFileName -Force  
    

    ==========
    Updated ==========
    To modify the "Title" field of a document:

    #Load SharePoint CSOM Assemblies  
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"  
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  
        
    #Set parameter values  
    $SiteURL="https://contoso.sharepoint.com/sites/teamsite"  
    $FileRelativeUrl="/sites/teamsite/Shared Documents/Investment Process.pptx"  
       
    Try {  
            #Get Credentials to connect  
            $Cred= Get-Credential  
            $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)  
        
            #Setup the context  
            $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)  
            $Ctx.Credentials = $Credentials  
       
            $File = $Ctx.web.GetFileByServerRelativeUrl($FileRelativeUrl)  
            $Ctx.Load($File)  
            $Ctx.ExecuteQuery()  
       
            #Set Metadata of the File  
            $ListItem = $File.ListItemAllFields  
            $Listitem["Title"] = "NewTitleTest"  
            $ListItem.Update()  
            $Ctx.ExecuteQuery()  
       
            Write-host -f Green "File's title has been Updated Successfully!"  
         }  
        Catch {  
            write-host -f Red "Error Updating title of the File!" $_.Exception.Message  
       }  
    

    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.