Get the App
SLTechnology News&Howtos  ›  Servers  › 

Use powershell to delete files before the specified date and add them to the scheduled task

Shulou Source: shulou.com Published: 2022-06-02 13:19:43 09月15日 Update

The machine system used for the video conversion of the company meeting is windows. When the program conversion fails, temporary files will be generated on disk D, which over time will cause the disk to be full and the new video conference recording content cannot be converted. In order to avoid this situation, the corresponding delete and add scheduled task scripts are temporarily written with powershell.

System environment: Microsoft Windows Server 2012 R2 Standard

Powershell version: 4.0

1. Delete script

Here, put it under the root directory of the D disk, and the file name is D:\ delete_tmp_files.ps1. Delete the files and directories from 15 days ago.

Delete the script delete_tmp_files.ps1 as follows:

# delete old tmp files,just save files in 15 days~$TimeOutDays=15$ filePath= "D:\ tmp" $allFiles=get-childitem-path $filePath foreach ($files in $allFiles) {$daypan= ((get-date)-$files.lastwritetime). Days if ($daypan-gt $TimeOutDays) {remove-item $files.fullname-Recurse-force}}

-Recurse indicates recursion, and subdirectories can be deleted

-force forced deletion, can delete hidden and read-only files (delete all file force fully,delete all hidden or read-only files)

two。 Add scheduled task

Select the add_schedule_task.ps1 script and choose to run it using PowerShell with the right mouse button.

The powershell script add_schedule_task.ps1 that joins the scheduled task is as follows:

Ipmo PSScheduledJob $T = New-JobTrigger-Weekly-DaysOfWeek 0 Trigger 1, 2, 3, 4, 5, 5, 6-At 2:38AMRegister-ScheduledJob-Name Delete-Tmp-Files-FilePath "D:\ delete_tmp_files.ps1"-Trigger $T

-DaysOfWeek: in weekly scheduled tasks, specify the day of the week to run, which is usually used with-Weekly.

Sunday-- > Monday-- >.-- > Saturday can be expressed in English or in numbers, and the corresponding table is as follows:

The English SundayMondayTuesdayWednesdayThursdayFridaySaturday number 01234563 on Sunday, Monday, Tuesday, Wednesday, Thursday, Friday and Saturday. View scheduled tasks

Click Server Manager-- > Task Scheduler-- > Microsoft-- > Windows-- > PowerShell-- > ScheduledJobs

Reference:

Use powershell to delete files earlier than a specified date

Delete-files-older-than-15-days-using-powershell

HOW TO CREATE SCHEDULE TASK USING POWERSHELL

New-JobTrigger

How-to-delete-a-folder-or-file-using-powershell

Tags: Tasks files scripts content meetings numbers directories programs systems videos English runs dates days companies subdirectories subdirectories situations methods servers Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Shulou Technology MariaDB macOS Microsoft