powershellbackuppowershell-isegetdate

Move Files to a Backup Folder every first in the month and Zip the Files


I have never worked with powershell and I only need a program that is always run at the beginning of the month

What should the program do?

At the beginning of the month it should move all data except for the last month to another folder and zip it there to save storage space.

to my question i have (-31 days) but not every month has 31 days how could i solve it or does it fit like that?

I'm sorry if I explained something wrong here, please let me know.

foreach ($kunde in (Get-ChildItem "C:\Users\kaar\Desktop\Ordneralt" -Exclude *.pdf , *.jpeg, *.png, *.gif ))
{
       $dirname = $kunde.name 
            for ($i=0; $i -lt $dirname.length; $i++)
                {
                    $dirbackup = $dirname + "BP"
                    get-childitem -Path "C:\Users\kaar\Desktop\Ordneralt\$dirname\ARCHIV" |
                    where-object {$_.LastWriteTime -lt (get-date).AddDays(-31)} | 
                    move-item -destination "C:\Users\kaar\Desktop\Ordnerneu\$dirbackup"
                }
                    

            $dirdate = get-date -format 'yyyy-MM-dd' 
            $backupname = $dirdate + "__" +  $dirname

                    Compress-Archive -Path "C:\Users\kaar\Desktop\Ordnerneu\$dirbackup" -DestinationPath "C:\Users\kaar\Desktop\Ordnerneu\$dirbackup\$backupname"
                    Remove-Item "C:\Users\kaar\Desktop\Ordnerneu\$dirbackup" -Recurse -Include *.pdf -force
}

i was searching for ideas but i didnt find nothing


Solution

  • Thanks guys!

    the program will get executed every first of the month and i managed it like that. that worked for me!

    $tage =(Get-Date).adddays(-1).day +1 #  value of days from month before
                    
                    $dirbackup = $dirname + "BP"
                    get-childitem -Path "C:\Users\kaar\Desktop\Ordneralt\$dirname\ARCHIV" | 
                    where-object {$_.LastWriteTime -lt (get-date).AddDays(-$tage)} | 
                    move-item -destination "C:\Users\kaar\Desktop\Ordnerneu\Temp"