Hope someone can help, I'm new to PowerShell an can't seem to get this working the way I want.
I'm trying to create a PowerShell script to run every morning to check in a folder for a file that was created that morning, the file will have a creation date of the day before, for example 2022-04-20_test.csv
The list of files can grow in the folder so I only am trying to copy over the one file that was created in the morning with the previous days date on it.
The script I've been testing copies the file over but is not checking the date correctly so every file in the folder called *_test.csv gets moved.
is there a way of me checking the file name with the previous days date or checking when the file was created date?
$EarliestModifiedTime = (Get-Date).AddDays(-1)
I thought this would check for all yesterday only files.
Any help or suggestions would be gratefully received. thanks
$DestinationFolder = "c:\test1"
If(!(test-path $DestinationFolder))
{New-Item -ItemType Directory -Force -Path $DestinationFolder
}
$EarliestModifiedTime = (Get-date).AddDays(-1)
$Files = Get-ChildItem "c:\test\*_test.csv" -File
foreach ($File in $Files)
{
if ($File.LastWriteTime -gt $EarliestModifiedTime)
{
Copy-Item $File -Destination $DestinationFolder
Write-Host "Copying $File"
}
else
{
Write-Host "Not copying $File"
}
}