When the number of files/folders in an organization increases, the administrators might need to delete outdated files. With more and more machines getting remote, admins find this task increasingly difficult. Hexnode lets admins handle this action via scripts on Windows devices.
Note:
Supported Versions:
The scripts given below will be supported on the following versions:
Windows 10 v1607+ (Pro, Enterprise, Education)
Windows 11 (Pro, Enterprise, Education)
Disclaimer:
The sample scripts provided below are adapted from third-party open-source sites.
To delete a file
Batch Script
Batch script to delete a file
1
del"filename_with_location.ext"
E.g., To delete the file test.txt in the Desktop of the user Deborah,
del “C:\Users\Deborah\Desktop\test.txt”
PowerShell Script
PowerShell script to delete a file
1
remove-item"filename.ext"
E.g., To delete the file test.txt in the Desktop of the user Deborah,
remove-item “C:\Users\Deborah\Desktop\test.txt”
Notes:
The admin can also delete files older than a specific number of days.
The .bat script to delete .docx files that are older than 20 days from the location C:\Users\username\Documents\My Blogs is as follows:
Batch script to delete files older than 20 days
1
2
3
@echo off forfiles/p"C:\Users\username\Documents\My Blogs"/s/m *.docx/d-20/c"cmd /c del @path"echo Document files older than20days deleted
pause
exit
The.ps1 script to delete files older than 20 days is as follows:
PowerShell script to delete files older than 20 days