'How to copy the "lastest created file" from a "source location" towards a Sharepoint space

I want to copy the lastest file that was uploded to a Path directory and then copy it towards another path location for reporting purposes. The code I’m using to attemp accomplish this action is the next one:

$filterDate = (Get-Date).AddDays(-5).Date
$Files = Get-ChildItem -Path "C:\repo\scripts\Copy move files\source\*.*" -File -ErrorAction SilentlyContinue | Where-Object {$_.TimeCreated -ge $filterDate}
foreach($File in $Files){
    Copy-Item -Destination "C:\repo\scripts\Copy move files\target\" $File.FullName
}


Solution 1:[1]

There is a PNP module created to cover this requirement with Powershell to interact with Sharepoint. Install the module with the next command:

Install-Module -Name "PnP.PowerShell" -Force

Declare your variables:

$id = "MyID"
$pass = "MyPass"
$site =  "MySharepointSpace"
Connect-PnPOnline -Url $site -ClientId $id -ClientSecret $secret

Finally, add the source and target based in the PNPOnline framework

$Files = Get-ChildItem -Path $path foreach($File in $Files){ Add-PnPFile -Folder "Shared Documents/XXX/XXX_XXX" -Path $File.FullName }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 apl