I hope you are well.
I am new in Powershell and am trying to make a script that allow to list files 7 days old based on the date in the filename, then copy/move them to another directory.
File Structure: 2020_06_11_DB.txt YYYY_MM_dd
I have this piece of script set up but it only match the file with the reference date (DateRef) and not the least (old) of it:
$SourceDB = "C:\DB\"
$DestinationDB = "C:\Destination\DB\"
$DateRef = Get-Date ((Get-Date).AddDays(-7)) -Format yyyy_MM_dd
$Query = Get-ChildItem $SourceDB -Filter "*$DateRef*" |
ForEach-object {
Copy-Item -Path $_.FullName -Destination $DestinationDB
}
I tried operators like -le and -lt but with no success. I would appreciate if you can help me.
Get-ChildItem $SourceDB -Filter '*.txt' | Where-Object {$_.BaseName -le "*$DateRef*"}
Thank you!