Powershell script - Sharepoint- set metadata values for more than one item.

Nigel Foot 76 Reputation points
2022-05-31T23:06:20.76+00:00

Hi, I have a script that will set the metadata field values of an individual "named" file in a library in a "target" site. I would like the script to check for multiple files with a modified date of within the last 24hours and be filtered with an Author's email address.

This script works fine for an individual files (sitepage) but I would like to NOT have to specify the values specifically in the parameters for the script and just be able to run the script and it gets the values of items (site pages) that have been modified in the last 24 hours from a list (https://tennant.sharepoint.com/sites/xxxxxx/xxxxx/sitepages). The script would then need to update the metadata Title, Author, and Editor with the Author's name, which can be specified in the parameters of the script.

This is what my working script looks like so far :-

Parameters

$SiteURL = "https://tenant.sharepoint.com/sites/xxxxxxx"
$ListName = "sitepages"
$SiteRelativeURL = "/sitepages/filename.aspx"
$CreatedBy = "first.last@keyman .com"
$Title = "filename.aspx"

Connect to PnP Online

Connect-PnPOnline -Url $SiteURL -Interactive

Get the File from SharePoint

$File = Get-PnPFile -Url $SiteRelativeURL -AsListItem

Update document properties

Set-PnPListItem -List $ListName -Identity $File.Id -Values @{"Title" = $Title; "Author" = $CreatedBy; "Editor" = $CreatedBy}

It does work but I need to be less specific with the values in #parameters and have it get the values itself and return the filename, path and filter by files modified in the last 24hours. As I say I can put in the email address myself as this value will change from time to time.

TIA for any help you may be able to offer me.

Nigel

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,073 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,435 questions
{count} votes

Accepted answer
  1. RaytheonXie_MSFT 32,801 Reputation points Microsoft Vendor
    2022-06-01T08:39:21.157+00:00

    Hi @Nigel Foot ,
    Per my test, I use the following script successfully set the Creator and Author of the last 24 hours update files which Author's email address equals aaa@xxx.onmicrosoft.com

     #Config Parameters  
     $SiteURL = "https://xxx.sharepoint.com/sites/xiexin"  
     $LibraryName = "Document Lib"  
     $Title = "filename.aspx"  
     $EmailAddress = "aaa@xxx.onmicrosoft.com"  
     $CreatedBy = "bbb@xxx.onmicrosoft.com"     
            
     Connect-PnPOnline -Url $SiteURL -Interactive  
     $List  = Get-PnPList -Identity $LibraryName  
     $ListItems = Get-PnPListItem -List $LibraryName  
      
     Foreach ($Item in $ListItems){  
        $Name = $Item["FileLeafRef"]  
        $Modified = $Item["Modified"]  
        $nowtime = get-date  
        $ModifiedTime = ($nowtime - $Modified).totalhours  
        $CreatorEmail = $Item["Author"].Email  
      
      
        If(($ModifiedTime -le 24) -and ($EmailAddress -eq $CreatorEmail)){  
            Set-PnPListItem -List $LibraryName -Identity $Item.Id -Values @{"Title" = $Title; "Author" = $CreatedBy; "Editor" = $CreatedBy}  
        }  
     }  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    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.


    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Nigel Foot 76 Reputation points
    2022-06-02T10:43:00.367+00:00

    Hi @RaytheonXie_MSFT ,

    I have tried to modify your working script slightly so that I can enter the information when asked and still run the script. Unfortunately I cant seem to get this to work correctly. When I run the script I get the following error:-

    207873-image.png

    Here is the script modified, wondered if you had any idea why this happens please?

    Do{  
    #Config Parameters for setting the file Metadata  
      $Tenanturl = "https://your-tenantsharepoint.com"  
      $TargetSiteUrl = Read-Host "Enter the Destination Site Url (EXAMPLE: https://your-tenant.sharpoint.com/xxxx/xxxx)"  
      $LibraryName = "sitePages"  
      $EmailAddress = "firstname.surname@yourdomain.com"  
      $CreatedBy = Read-Host "Enter the Author's Email Address"  
      
      Write-Host "The entered Tenant Site Url is:" $Tenanturl/$TargetSiteurl -ForegroundColor Green  
      Write-Host "The entered Destination Library is:" $LibraryName -ForegroundColor Green  
      Write-Host "The entered new author's email Address is:" $CreatedBy -ForegroundColor Green  
      
      $Confirmation = Read-Host -Prompt "Is this the correct Site information and email address? (y/n)"  
    }  
      
    while ($Confirmation -ne "y")  
      
    Connect-PnPOnline -Url "$tenanturl/$TargetSiteUrl" -Interactive    
    Write-Host "Connected - Please wait while information you entered is assessed....." -ForegroundColor Green  
      
      
      $List  = Get-PnPList -Identity $LibraryName  
      $ListItems = Get-PnPListItem -List $LibraryName  
          
      Foreach ($Item in $ListItems){  
         $Name = $Item["Title"]  
         $Modified = $Item["Modified"]  
         $nowtime = get-date  
         $ModifiedTime = ($nowtime - $Modified).totalhours  
         $CreatorEmail = $Item["Author"].Email  
         $Title = $Name  
          
          
         If(($ModifiedTime -le 24) -and ($EmailAddress -eq $CreatorEmail)){  
             Set-PnPListItem -List $LibraryName -Identity $Item.Id -Values @{"Title" = $Title; "Author" = $CreatedBy; "Editor" = $CreatedBy}  
         }  
      }  
      
    Write-Host "The Metadata for files in Target Library is now set." -ForegroundColor Green  
    Pause  
    

    TIA

    Nigel


  2. Nigel Foot 76 Reputation points
    2022-06-02T11:28:53.54+00:00

    @RaytheonXie_MSFT - I have managed to sort this now. I put quotes around the Createdby in the Set-PnpListItem section of the script.

    So now it looks like this and this works for me.

    Do{  
    #Config Parameters for setting the file Metadata  
      $Tenanturl = "https://your-tenant.sharepoint.com"  
      $TargetSiteUrl = Read-Host "Enter the Destination Site Url (EXAMPLE: sites/365training)"  
      $LibraryName = Read-Host "Enter the library name (EXAMPLE: sitepages)"  
      $EmailAddress = "first.last@your-domain.com"  
      $CreatedBy = Read-Host "Enter the new author's Email Address"  
      
      Write-Host "The entered Tenant Site Url is:" $Tenanturl/$TargetSiteurl -ForegroundColor Green  
      Write-Host "The entered Destination Library is:" $LibraryName -ForegroundColor Green  
      Write-Host "The entered new author's email Address is:" $CreatedBy -ForegroundColor Green  
      
      $Confirmation = Read-Host -Prompt "Is this the correct Site inforation and email address? (y/n)"  
    }  
      
    while ($Confirmation -ne "y")  
      
    Connect-PnPOnline -Url "$tenanturl/$TargetSiteUrl" -Interactive    
    Write-Host "Connected - Please wait while information you entered is assessed....." -ForegroundColor Green  
      
      $List  = Get-PnPList -Identity $LibraryName  
      $ListItems = Get-PnPListItem -List $LibraryName  
          
      Foreach ($Item in $ListItems){  
         $Name = $Item["Title"]  
         $Modified = $Item["Modified"]  
         $nowtime = get-date  
         $ModifiedTime = ($nowtime - $Modified).totalhours  
         $CreatorEmail = $Item["Author"].Email  
         $Title = $Name       
          
         If(($ModifiedTime -le 24) -and ($EmailAddress -eq $CreatorEmail)){  
             Set-PnPListItem -List $LibraryName -Identity $Item.Id -Values @{"Title" = $Title; "Author" = "$CreatedBy"; "Editor" = "$CreatedBy"}  
         }  
      }  
      
    Write-Host "The Metadata for files in Target Library is now set." -ForegroundColor Green  
    Pause  
    
    0 comments No comments